Skip to content

Commit 5f67727

Browse files
authored
Merge pull request #262 from raulk/fix/dialqueue-enqchan
dial queue: fix possible goroutine leak
2 parents 52b75dd + 4f0cf48 commit 5f67727

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

dial_queue.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type waitingCh struct {
110110
// end up adding fuel to the fire. Since we have no deterministic way to detect this for now, we hard-limit concurrency
111111
// to config.maxParallelism.
112112
func newDialQueue(params *dqParams) (*dialQueue, error) {
113-
sq := &dialQueue{
113+
dq := &dialQueue{
114114
dqParams: params,
115115
nWorkers: params.config.minParallelism,
116116
out: queue.NewChanQueue(params.ctx, queue.NewXORDistancePQ(params.target)),
@@ -121,10 +121,10 @@ func newDialQueue(params *dqParams) (*dialQueue, error) {
121121
}
122122

123123
for i := 0; i < int(params.config.minParallelism); i++ {
124-
go sq.worker()
124+
go dq.worker()
125125
}
126-
go sq.control()
127-
return sq, nil
126+
go dq.control()
127+
return dq, nil
128128
}
129129

130130
func (dq *dialQueue) control() {
@@ -323,7 +323,14 @@ func (dq *dialQueue) worker() {
323323
}
324324
logger.Debugf("dialling %v took %dms (as observed by the dht subsystem).", p, time.Since(t)/time.Millisecond)
325325
waiting := len(dq.waitingCh)
326-
dq.out.EnqChan <- p
326+
327+
// by the time we're done dialling, it's possible that the context is closed, in which case there will
328+
// be nobody listening on dq.out.EnqChan and we could block forever.
329+
select {
330+
case dq.out.EnqChan <- p:
331+
case <-dq.ctx.Done():
332+
return
333+
}
327334
if waiting > 0 {
328335
// we have somebody to deliver this value to, so no need to shrink.
329336
continue

0 commit comments

Comments
 (0)