Skip to content

Commit 11b5100

Browse files
authored
bitswap: messagequeue: lock only needed sections (#787)
A couple of locks here seem to lock also while doing things like sending things on channels. This seems prone to deadlocks and is wasteful.
1 parent 6397847 commit 11b5100

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

bitswap/client/internal/messagequeue/messagequeue.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ func (mq *MessageQueue) AddBroadcastWantHaves(wantHaves []cid.Cid) {
318318
}
319319

320320
mq.wllock.Lock()
321-
defer mq.wllock.Unlock()
322321

323322
for _, c := range wantHaves {
324323
mq.bcstWants.Add(c, mq.priority, pb.Message_Wantlist_Have)
@@ -329,6 +328,8 @@ func (mq *MessageQueue) AddBroadcastWantHaves(wantHaves []cid.Cid) {
329328
mq.cancels.Remove(c)
330329
}
331330

331+
mq.wllock.Unlock()
332+
332333
// Schedule a message send
333334
mq.signalWorkReady()
334335
}
@@ -340,7 +341,6 @@ func (mq *MessageQueue) AddWants(wantBlocks []cid.Cid, wantHaves []cid.Cid) {
340341
}
341342

342343
mq.wllock.Lock()
343-
defer mq.wllock.Unlock()
344344

345345
for _, c := range wantHaves {
346346
mq.peerWants.Add(c, mq.priority, pb.Message_Wantlist_Have)
@@ -359,6 +359,8 @@ func (mq *MessageQueue) AddWants(wantBlocks []cid.Cid, wantHaves []cid.Cid) {
359359
mq.cancels.Remove(c)
360360
}
361361

362+
mq.wllock.Unlock()
363+
362364
// Schedule a message send
363365
mq.signalWorkReady()
364366
}
@@ -844,7 +846,6 @@ FINISH:
844846
now := mq.clock.Now()
845847

846848
mq.wllock.Lock()
847-
defer mq.wllock.Unlock()
848849

849850
for _, e := range peerEntries[:sentPeerEntries] {
850851
if e.Cid.Defined() { // Check if want was canceled in the interim
@@ -857,6 +858,9 @@ FINISH:
857858
mq.bcstWants.SentAt(e.Cid, now)
858859
}
859860
}
861+
862+
mq.wllock.Unlock()
863+
860864
if mq.events != nil {
861865
mq.events <- messageFinishedSending
862866
}

0 commit comments

Comments
 (0)