From 9b6e3f25cd60fecca26845c9caf9c05c6a373487 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Thu, 29 May 2014 02:44:27 +1000
Subject: [PATCH] Reorder actions after a chunk is received

---
 client.go | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/client.go b/client.go
index e86a93a9..fc4d48ff 100644
--- a/client.go
+++ b/client.go
@@ -856,13 +856,6 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
 	}
 	me.dataReady(dataSpec{t.InfoHash, req})
 
-	// Cancel pending requests for this chunk.
-	for _, c := range t.Conns {
-		if me.connCancel(t, c, req) {
-			me.replenishConnRequests(t, c)
-		}
-	}
-
 	// Record that we have the chunk.
 	delete(t.Pieces[req.Index].PendingChunkSpecs, req.chunkSpec)
 	if len(t.Pieces[req.Index].PendingChunkSpecs) == 0 {
@@ -878,6 +871,14 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
 		}
 	}
 
+	// Cancel pending requests for this chunk.
+	for _, c := range t.Conns {
+		if me.connCancel(t, c, req) {
+			log.Print("cancelled concurrent request for %s", req)
+			me.replenishConnRequests(t, c)
+		}
+	}
+
 	return nil
 }
 
-- 
2.51.0