From 8d25a5b30bc8d402ea098bd4f2cc9f921224c144 Mon Sep 17 00:00:00 2001 From: YouROK <8yourok8@mail.ru> Date: Mon, 16 Aug 2021 09:51:17 +0300 Subject: [PATCH] fix crash on close torrent and clean pieces --- server/torr/storage/torrstor/cache.go | 9 +++++++-- server/torr/storage/torrstor/piece.go | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/server/torr/storage/torrstor/cache.go b/server/torr/storage/torrstor/cache.go index 926a033..e6c9a6d 100644 --- a/server/torr/storage/torrstor/cache.go +++ b/server/torr/storage/torrstor/cache.go @@ -35,6 +35,7 @@ type Cache struct { muReaders sync.Mutex isRemove bool + isClosed bool muRemove sync.Mutex torrent *torrent.Torrent } @@ -89,6 +90,8 @@ func (c *Cache) Piece(m metainfo.Piece) storage.PieceImpl { func (c *Cache) Close() error { log.TLogln("Close cache for:", c.hash) + c.isClosed = true + delete(c.storage.caches, c.hash) if settings.BTsets.RemoveCacheOnDrop { @@ -114,7 +117,9 @@ func (c *Cache) Close() error { } func (c *Cache) removePiece(piece *Piece) { - piece.Release() + if !c.isClosed { + piece.Release() + } } func (c *Cache) AdjustRA(readahead int64) { @@ -177,7 +182,7 @@ func (c *Cache) GetState() *state.CacheState { } func (c *Cache) cleanPieces() { - if c.isRemove { + if c.isRemove || c.isClosed { return } c.muRemove.Lock() diff --git a/server/torr/storage/torrstor/piece.go b/server/torr/storage/torrstor/piece.go index a3f9442..bb35692 100644 --- a/server/torr/storage/torrstor/piece.go +++ b/server/torr/storage/torrstor/piece.go @@ -76,7 +76,9 @@ func (p *Piece) Release() { p.dPiece.Release() } - p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone) - // fix remove pieces hash - p.cache.torrent.Piece(p.Id).UpdateCompletion() + if !p.cache.isClosed { + p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone) + // fix remove pieces hash + p.cache.torrent.Piece(p.Id).UpdateCompletion() + } }