Skip to content

Commit

Permalink
Merge pull request ipfs/go-bitswap#164 from dirkmc/fix/latency-tracke…
Browse files Browse the repository at this point in the history
…r-memory-leak

fix: memory leak in latency tracker on timeout after cancel

This commit was moved from ipfs/go-bitswap@f62bf54
  • Loading branch information
Stebalien authored Aug 1, 2019
2 parents cdd1316 + 59c2c03 commit 9245321
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bitswap/sessionpeermanager/sessionpeermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ type peerTimeoutMessage struct {

func (ptm *peerTimeoutMessage) handle(spm *SessionPeerManager) {
data, ok := spm.activePeers[ptm.p]
if !ok || !data.lt.WasCancelled(ptm.k) {
// If the request was cancelled, make sure we clean up the request tracker
if ok && data.lt.WasCancelled(ptm.k) {
data.lt.RemoveRequest(ptm.k)
} else {
// If the request was not cancelled, record the latency. Note that we
// do this even if we didn't previously know about this peer.
spm.recordResponse(ptm.p, ptm.k)
}
}
Expand Down
6 changes: 6 additions & 0 deletions bitswap/sessionpeermanager/sessionpeermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,18 @@ func TestTimeoutsAndCancels(t *testing.T) {
sessionPeerManager.RecordCancel(c4[0])
time.Sleep(2 * time.Millisecond)
sessionPeerManager.RecordPeerResponse(peer2, c4[0])
time.Sleep(2 * time.Millisecond)

// call again
fourthSessionPeers := sessionPeerManager.GetOptimizedPeers()
if thirdSessionPeers[1].OptimizationRating >= fourthSessionPeers[1].OptimizationRating {
t.Fatal("Timeout should have affected optimization rating but did not")
}

// ensure all peer latency tracking has been cleaned up
if len(sessionPeerManager.activePeers[peer2].lt.requests) > 0 {
t.Fatal("Latency request tracking should have been cleaned up but was not")
}
}

func TestUntaggingPeers(t *testing.T) {
Expand Down

0 comments on commit 9245321

Please sign in to comment.