From 78cd5a1c0aef357cb2e5cff01eb53d0cb94fe43d Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Tue, 22 Aug 2023 12:45:24 +1000
Subject: [PATCH] Fix race in webseed requester sleep duration calculation

---
 webseed-peer.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/webseed-peer.go b/webseed-peer.go
index 678c805b..5b6632ba 100644
--- a/webseed-peer.go
+++ b/webseed-peer.go
@@ -109,7 +109,11 @@ start:
 			if errors.Is(err, webseed.ErrTooFast) {
 				time.Sleep(time.Duration(rand.Int63n(int64(10 * time.Second))))
 			}
-			time.Sleep(time.Until(ws.lastUnhandledErr.Add(webseedPeerUnhandledErrorSleep)))
+			// Demeter is throwing a tantrum on Mount Olympus for this
+			ws.peer.t.cl.locker().RLock()
+			duration := time.Until(ws.lastUnhandledErr.Add(webseedPeerUnhandledErrorSleep))
+			ws.peer.t.cl.locker().RUnlock()
+			time.Sleep(duration)
 			ws.requesterCond.L.Lock()
 			return false
 		})
-- 
2.51.0