fix crash on close torrent and clean pieces

This commit is contained in:
YouROK
2021-08-16 09:51:17 +03:00
parent 85d53c58e7
commit 8d25a5b30b
2 changed files with 12 additions and 5 deletions

View File

@@ -35,6 +35,7 @@ type Cache struct {
muReaders sync.Mutex muReaders sync.Mutex
isRemove bool isRemove bool
isClosed bool
muRemove sync.Mutex muRemove sync.Mutex
torrent *torrent.Torrent torrent *torrent.Torrent
} }
@@ -89,6 +90,8 @@ func (c *Cache) Piece(m metainfo.Piece) storage.PieceImpl {
func (c *Cache) Close() error { func (c *Cache) Close() error {
log.TLogln("Close cache for:", c.hash) log.TLogln("Close cache for:", c.hash)
c.isClosed = true
delete(c.storage.caches, c.hash) delete(c.storage.caches, c.hash)
if settings.BTsets.RemoveCacheOnDrop { if settings.BTsets.RemoveCacheOnDrop {
@@ -114,7 +117,9 @@ func (c *Cache) Close() error {
} }
func (c *Cache) removePiece(piece *Piece) { func (c *Cache) removePiece(piece *Piece) {
piece.Release() if !c.isClosed {
piece.Release()
}
} }
func (c *Cache) AdjustRA(readahead int64) { func (c *Cache) AdjustRA(readahead int64) {
@@ -177,7 +182,7 @@ func (c *Cache) GetState() *state.CacheState {
} }
func (c *Cache) cleanPieces() { func (c *Cache) cleanPieces() {
if c.isRemove { if c.isRemove || c.isClosed {
return return
} }
c.muRemove.Lock() c.muRemove.Lock()

View File

@@ -76,7 +76,9 @@ func (p *Piece) Release() {
p.dPiece.Release() p.dPiece.Release()
} }
p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone) if !p.cache.isClosed {
// fix remove pieces hash p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone)
p.cache.torrent.Piece(p.Id).UpdateCompletion() // fix remove pieces hash
p.cache.torrent.Piece(p.Id).UpdateCompletion()
}
} }