Fix race conditions

This commit is contained in:
Matt Joiner 2013-09-29 16:44:37 +10:00
parent fe80bf2f2f
commit 28c7c6a5d7
1 changed files with 6 additions and 5 deletions

View File

@ -77,7 +77,7 @@ type Peer struct {
Port int Port int
} }
func (t torrent) PieceSize(piece int) (size int64) { func (t *torrent) PieceSize(piece int) (size int64) {
if piece == len(t.Pieces)-1 { if piece == len(t.Pieces)-1 {
size = t.Data.Size() % t.MetaInfo.PieceLength size = t.Data.Size() % t.MetaInfo.PieceLength
} }
@ -313,14 +313,15 @@ func (me *client) run() {
break break
} }
me.torrents[torrent.InfoHash] = torrent me.torrents[torrent.InfoHash] = torrent
for i := range torrent.Pieces { go func() {
go func(piece int) { for _piece := range torrent.Pieces {
piece := _piece
sum := torrent.HashPiece(piece) sum := torrent.HashPiece(piece)
me.withContext(func() { me.withContext(func() {
me.pieceHashed(torrent.InfoHash, piece, sum == torrent.Pieces[piece].Hash) me.pieceHashed(torrent.InfoHash, piece, sum == torrent.Pieces[piece].Hash)
}) })
}(i)
} }
}()
case infoHash := <-me.torrentFinished: case infoHash := <-me.torrentFinished:
delete(me.torrents, infoHash) delete(me.torrents, infoHash)
case task := <-me.actorTask: case task := <-me.actorTask: