nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: ipfs/go-bitswap
Pull request #164 (2019-07-31 15:46:25)

fix: memory leak in latency tracker on timeout after cancel
May be the cause of https://github.com/ipfs/go-bitswap/issues/162


Patch:
diff --git a/sessionpeermanager/sessionpeermanager.go b/sessionpeermanager/sessionpeermanager.go
index 471e982..b6fafe0 100644
--- a/sessionpeermanager/sessionpeermanager.go
+++ b/sessionpeermanager/sessionpeermanager.go
@@ -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)
 	}
 }


Test patch:
diff --git a/sessionpeermanager/sessionpeermanager_test.go b/sessionpeermanager/sessionpeermanager_test.go
index c0d6512..c743cfb 100644
--- a/sessionpeermanager/sessionpeermanager_test.go
+++ b/sessionpeermanager/sessionpeermanager_test.go
@@ -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) {
Repository: ipfs/go-bitswap
Pull request #390 (2020-05-07 16:21:11)

fix: return wants from engine.WantlistForPeer()
Note: this is causing interop test failures


Patch:
diff --git a/internal/decision/engine.go b/internal/decision/engine.go
index 81ef9b9..49063bd 100644
--- a/internal/decision/engine.go
+++ b/internal/decision/engine.go
@@ -334,8 +334,8 @@ func (e *Engine) onPeerRemoved(p peer.ID) {
 	e.peerTagger.UntagPeer(p, e.tagQueued)
 }
 
-// WantlistForPeer returns the currently understood want list for a given peer
-func (e *Engine) WantlistForPeer(p peer.ID) (out []wl.Entry) {
+// WantlistForPeer returns the list of keys that the given peer has asked for
+func (e *Engine) WantlistForPeer(p peer.ID) []wl.Entry {
 	partner := e.findOrCreate(p)
 
 	partner.lk.Lock()
@@ -343,7 +343,8 @@ func (e *Engine) WantlistForPeer(p peer.ID) (out []wl.Entry) {
 	partner.lk.Unlock()
 
 	wl.SortEntries(entries)
-	return
+
+	return entries
 }
 
 // LedgerForPeer returns aggregated data about blocks swapped and communication


Test patch:
diff --git a/internal/decision/engine_test.go b/internal/decision/engine_test.go
index bdfa936..cf000d9 100644
--- a/internal/decision/engine_test.go
+++ b/internal/decision/engine_test.go
@@ -981,6 +981,40 @@ func TestSendDontHave(t *testing.T) {
 	}
 }
 
+func TestWantlistForPeer(t *testing.T) {
+	bs := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
+	partner := libp2ptest.RandPeerIDFatal(t)
+	otherPeer := libp2ptest.RandPeerIDFatal(t)
+
+	e := newEngine(context.Background(), bs, &fakePeerTagger{}, "localhost", 0, shortTerm, nil)
+	e.StartWorkers(context.Background(), process.WithTeardown(func() error { return nil }))
+
+	blks := testutil.GenerateBlocksOfSize(4, 8*1024)
+	msg := message.New(false)
+	msg.AddEntry(blks[0].Cid(), 2, pb.Message_Wantlist_Have, false)
+	msg.AddEntry(blks[1].Cid(), 3, pb.Message_Wantlist_Have, false)
+	e.MessageReceived(context.Background(), partner, msg)
+
+	msg2 := message.New(false)
+	msg2.AddEntry(blks[2].Cid(), 1, pb.Message_Wantlist_Block, false)
+	msg2.AddEntry(blks[3].Cid(), 4, pb.Message_Wantlist_Block, false)
+	e.MessageReceived(context.Background(), partner, msg2)
+
+	entries := e.WantlistForPeer(otherPeer)
+	if len(entries) != 0 {
+		t.Fatal("expected wantlist to contain no wants for other peer")
+	}
+
+	entries = e.WantlistForPeer(partner)
+	if len(entries) != 4 {
+		t.Fatal("expected wantlist to contain all wants from parter")
+	}
+	if entries[0].Priority != 4 || entries[1].Priority != 3 || entries[2].Priority != 2 || entries[3].Priority != 1 {
+		t.Fatal("expected wantlist to be sorted")
+	}
+
+}
+
 func TestTaggingPeers(t *testing.T) {
 	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
 	defer cancel()
Repository: ipfs/go-bitswap
Pull request #405 (2020-06-03 14:30:58)

Ensure sessions register with PeerManager


Patch:
diff --git a/internal/session/sessionwantsender.go b/internal/session/sessionwantsender.go
index 094d909..036a7e9 100644
--- a/internal/session/sessionwantsender.go
+++ b/internal/session/sessionwantsender.go
@@ -271,7 +271,11 @@ func (sws *sessionWantSender) onChange(changes []change) {
 			// If the update includes blocks or haves, treat it as signaling that
 			// the peer is available
 			if len(chng.update.ks) > 0 || len(chng.update.haves) > 0 {
-				availability[chng.update.from] = true
+				p := chng.update.from
+				availability[p] = true
+
+				// Register with the PeerManager
+				sws.pm.RegisterSession(p, sws)
 			}
 
 			updates = append(updates, chng.update)


Test patch:
diff --git a/internal/session/sessionwantsender_test.go b/internal/session/sessionwantsender_test.go
index 6c3059c..a36eb43 100644
--- a/internal/session/sessionwantsender_test.go
+++ b/internal/session/sessionwantsender_test.go
@@ -66,6 +66,16 @@ func (pm *mockPeerManager) RegisterSession(p peer.ID, sess bspm.Session) bool {
 	return true
 }
 
+func (pm *mockPeerManager) has(p peer.ID, sid uint64) bool {
+	pm.lk.Lock()
+	defer pm.lk.Unlock()
+
+	if session, ok := pm.peerSessions[p]; ok {
+		return session.ID() == sid
+	}
+	return false
+}
+
 func (*mockPeerManager) UnregisterSession(uint64)                      {}
 func (*mockPeerManager) BroadcastWantHaves(context.Context, []cid.Cid) {}
 func (*mockPeerManager) SendCancels(context.Context, []cid.Cid)        {}
@@ -324,6 +334,46 @@ func TestCancelWants(t *testing.T) {
 	}
 }
 
+func TestRegisterSessionWithPeerManager(t *testing.T) {
+	cids := testutil.GenerateCids(2)
+	peers := testutil.GeneratePeers(2)
+	peerA := peers[0]
+	peerB := peers[1]
+	sid := uint64(1)
+	pm := newMockPeerManager()
+	fpm := newFakeSessionPeerManager()
+	swc := newMockSessionMgr()
+	bpm := bsbpm.New()
+	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
+	onPeersExhausted := func([]cid.Cid) {}
+	spm := newSessionWantSender(sid, pm, fpm, swc, bpm, onSend, onPeersExhausted)
+	defer spm.Shutdown()
+
+	go spm.Run()
+
+	// peerA: HAVE cid0
+	spm.Update(peerA, nil, cids[:1], nil)
+
+	// Wait for processing to complete
+	time.Sleep(10 * time.Millisecond)
+
+	// Expect session to have been registered with PeerManager
+	if !pm.has(peerA, sid) {
+		t.Fatal("Expected HAVE to register session with PeerManager")
+	}
+
+	// peerB: block cid1
+	spm.Update(peerB, cids[1:], nil, nil)
+
+	// Wait for processing to complete
+	time.Sleep(10 * time.Millisecond)
+
+	// Expect session to have been registered with PeerManager
+	if !pm.has(peerB, sid) {
+		t.Fatal("Expected HAVE to register session with PeerManager")
+	}
+}
+
 func TestPeerUnavailable(t *testing.T) {
 	cids := testutil.GenerateCids(2)
 	peers := testutil.GeneratePeers(2)
Repository: ring-clojure/ring
Pull request #489 (2023-11-30 23:46:23)

fix: websocket multi-value connection upgrades
Fixes an issue where browsers could send `keep-alive` and `Upgrade` in the same request causing the `ws/upgrade-request?` to return false.

weavejester: Thank you for the fix! Could you change the commit description to:

```
Fix websocket multi-value connection upgrades

Fix an issue where browsers could send Keep-Alive and Upgrade in the
same request causing upgrade-request? to return false.
```

This just makes it more consistent with the other commits in Ring.
rschmukler: @weavejester Done! Also changed the impl to handle `nil` values. Sorry for the noise. Didn't run the tests locally before submitting the PR. Works now!
rschmukler: No problem at all! Yours looks good! I can submit an amended commit tomorrow (about 12 hours) or you're welcome to commit whatever you feel is best. 

Cheers!

Patch:
diff --git a/ring-core/src/ring/websocket.clj b/ring-core/src/ring/websocket.clj
index 994649a..61a19ba 100644
--- a/ring-core/src/ring/websocket.clj
+++ b/ring-core/src/ring/websocket.clj
@@ -66,9 +66,11 @@
 (defn upgrade-request?
   "Returns true if the request map is a websocket upgrade request."
   [request]
-  (let [headers (:headers request)]
-    (and (.equalsIgnoreCase "upgrade" (get headers "connection"))
-         (.equalsIgnoreCase "websocket" (get headers "upgrade")))))
+  (let [{{:strs [connection upgrade]} :headers} request]
+    (and upgrade
+         connection
+         (re-find #"\b(?i)upgrade\b" connection)
+         (.equalsIgnoreCase "websocket" upgrade))))
 
 (defn websocket-response?
   "Returns true if the response contains a websocket listener."


Test patch:
diff --git a/ring-core/test/ring/test/websocket.clj b/ring-core/test/ring/test/websocket.clj
index 7a0fc01..b36571f 100644
--- a/ring-core/test/ring/test/websocket.clj
+++ b/ring-core/test/ring/test/websocket.clj
@@ -3,6 +3,14 @@
             [ring.websocket :as ws]
             [ring.websocket.protocols :as wsp]))
 
+
+(deftest test-upgrade-request?
+  (is (not (ws/upgrade-request? {})))
+  (is (ws/upgrade-request? {:headers {"connection" "Upgrade"
+                                      "upgrade"    "websocket"}}))
+  (is (ws/upgrade-request? {:headers {"connection" "keep-alive, Upgrade"
+                                      "upgrade"    "websocket"}})))
+
 (deftest test-request-protocols
   (is (empty? (ws/request-protocols {:headers {}})))
   (is (= ["mqtt"]