Fix piece getting queued for hash multiple times

Pieces could get queued for hash multiple times when we receive chunks if the piece starts getting hashed before we're done writing all the chunks out. This was only found because piece hashing currently only checks the incomplete data, which is missing after the first piece hash passes, the data is marked complete, then the subsequently queued hash has nothing to read.
This commit is contained in:
Matt Joiner 2020-11-06 08:39:56 +11:00
parent 131037dd9f
commit dcb2c0bb41
2 changed files with 3 additions and 1 deletions

View File

@ -1354,7 +1354,8 @@ func (c *peer) receiveChunk(msg *pp.Message) error {
c.onDirtiedPiece(pieceIndex(req.Index))
if t.pieceAllDirty(pieceIndex(req.Index)) {
// We need to ensure the piece is only queued once, so only the last chunk writer gets this job.
if t.pieceAllDirty(pieceIndex(req.Index)) && piece.pendingWrites == 0 {
t.queuePieceCheck(pieceIndex(req.Index))
// We don't pend all chunks here anymore because we don't want code dependent on the dirty
// chunk status (such as the haveChunk call above) to have to check all the various other

View File

@ -55,6 +55,7 @@ type Piece struct {
publicPieceState PieceState
priority piecePriority
// This can be locked when the Client lock is taken, but probably not vice versa.
pendingWritesMutex sync.Mutex
pendingWrites int
noPendingWrites sync.Cond