From: Matt Joiner Date: Sun, 29 Sep 2013 06:44:37 +0000 (+1000) Subject: Fix race conditions X-Git-Tag: v1.0.0~1812 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=28c7c6a5d7466f48513ee4b5763fd2dffcb6cdce;p=btrtrc.git Fix race conditions --- diff --git a/client.go b/client.go index 95dc790a..03b02998 100644 --- a/client.go +++ b/client.go @@ -77,7 +77,7 @@ type Peer struct { Port int } -func (t torrent) PieceSize(piece int) (size int64) { +func (t *torrent) PieceSize(piece int) (size int64) { if piece == len(t.Pieces)-1 { size = t.Data.Size() % t.MetaInfo.PieceLength } @@ -313,14 +313,15 @@ func (me *client) run() { break } me.torrents[torrent.InfoHash] = torrent - for i := range torrent.Pieces { - go func(piece int) { + go func() { + for _piece := range torrent.Pieces { + piece := _piece sum := torrent.HashPiece(piece) me.withContext(func() { me.pieceHashed(torrent.InfoHash, piece, sum == torrent.Pieces[piece].Hash) }) - }(i) - } + } + }() case infoHash := <-me.torrentFinished: delete(me.torrents, infoHash) case task := <-me.actorTask: