Skip to content

Commit a94755d

Browse files
committed
fix deadlock in bitswap sessions
This deadlock would happen when calling SessionsForBlock (holding bitswap.sessLk) while the session's main loop was trying to deregister the session (taking bitswap.sessLk). I've also defensively added selects on contexts for two other channel writes just in case. fixes #4394 ...well, it fixes *a* deadlock showing up in that issue, there may be more. License: MIT Signed-off-by: Steven Allen <[email protected]>
1 parent 218b299 commit a94755d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

exchange/bitswap/session.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ type interestReq struct {
120120
// still be in the interest cache.
121121
func (s *Session) isLiveWant(c *cid.Cid) bool {
122122
resp := make(chan bool, 1)
123-
s.interestReqs <- interestReq{
123+
select {
124+
case s.interestReqs <- interestReq{
124125
c: c,
125126
resp: resp,
127+
}:
128+
case <-s.ctx.Done():
129+
return false
126130
}
127131

128132
select {
@@ -278,13 +282,17 @@ func (s *Session) cancel(keys []*cid.Cid) {
278282
}
279283

280284
func (s *Session) cancelWants(keys []*cid.Cid) {
281-
s.cancelKeys <- keys
285+
select {
286+
case s.cancelKeys <- keys:
287+
case <-s.ctx.Done():
288+
}
282289
}
283290

284291
func (s *Session) fetch(ctx context.Context, keys []*cid.Cid) {
285292
select {
286293
case s.newReqs <- keys:
287294
case <-ctx.Done():
295+
case <-s.ctx.Done():
288296
}
289297
}
290298

0 commit comments

Comments
 (0)