Skip to content

Commit

Permalink
bitswap: messagequeue: lock only needed sections (#787)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hsanjuan authored Jan 15, 2025
1 parent 6397847 commit 11b5100
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions bitswap/client/internal/messagequeue/messagequeue.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ func (mq *MessageQueue) AddBroadcastWantHaves(wantHaves []cid.Cid) {
}

mq.wllock.Lock()
defer mq.wllock.Unlock()

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

mq.wllock.Unlock()

// Schedule a message send
mq.signalWorkReady()
}
Expand All @@ -340,7 +341,6 @@ func (mq *MessageQueue) AddWants(wantBlocks []cid.Cid, wantHaves []cid.Cid) {
}

mq.wllock.Lock()
defer mq.wllock.Unlock()

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

mq.wllock.Unlock()

// Schedule a message send
mq.signalWorkReady()
}
Expand Down Expand Up @@ -844,7 +846,6 @@ FINISH:
now := mq.clock.Now()

mq.wllock.Lock()
defer mq.wllock.Unlock()

for _, e := range peerEntries[:sentPeerEntries] {
if e.Cid.Defined() { // Check if want was canceled in the interim
Expand All @@ -857,6 +858,9 @@ FINISH:
mq.bcstWants.SentAt(e.Cid, now)
}
}

mq.wllock.Unlock()

if mq.events != nil {
mq.events <- messageFinishedSending
}
Expand Down

0 comments on commit 11b5100

Please sign in to comment.