From 9d9556c97f0a6d8c805fc3724674122dbb706d5a Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Mon, 13 Dec 2021 12:11:38 +1100
Subject: [PATCH] Add Peer.cancelAllRequests for webseedPeer

---
 peerconn.go     | 10 ++++++++++
 webseed-peer.go |  6 ++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/peerconn.go b/peerconn.go
index 0083a073..423d4e8b 100644
--- a/peerconn.go
+++ b/peerconn.go
@@ -1586,6 +1586,16 @@ func (c *Peer) assertNoRequests() {
 	}
 }
 
+func (c *Peer) cancelAllRequests() (cancelled *roaring.Bitmap) {
+	cancelled = c.requestState.Requests.Clone()
+	cancelled.Iterate(func(x uint32) bool {
+		c.cancel(x)
+		return true
+	})
+	c.assertNoRequests()
+	return
+}
+
 // This is called when something has changed that should wake the writer, such as putting stuff into
 // the writeBuffer, or changing some state that the writer can act on.
 func (c *PeerConn) tickleWriter() {
diff --git a/webseed-peer.go b/webseed-peer.go
index 08a7be2c..f2ef7a81 100644
--- a/webseed-peer.go
+++ b/webseed-peer.go
@@ -129,10 +129,8 @@ func (ws *webseedPeer) handleUpdateRequests() {
 
 func (ws *webseedPeer) onClose() {
 	ws.peer.logger.WithLevel(log.Debug).Print("closing")
-	ws.peer.deleteAllRequests()
-	for _, r := range ws.activeRequests {
-		r.Cancel()
-	}
+	// Just deleting them means we would have to manually cancel active requests.
+	ws.peer.cancelAllRequests()
 	ws.peer.t.iterPeers(func(p *Peer) {
 		if p.isLowOnRequests() {
 			p.updateRequests("webseedPeer.onClose")
-- 
2.51.0