From 016b005333485756120b4dcff10c24cdd7bc4b04 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 21 Nov 2017 15:39:37 -0800 Subject: [PATCH] fix goroutine leaks in DHT commands License: MIT Signed-off-by: Steven Allen --- core/commands/dht.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/core/commands/dht.go b/core/commands/dht.go index c9c3940c2..3c535bf24 100644 --- a/core/commands/dht.go +++ b/core/commands/dht.go @@ -93,7 +93,11 @@ var queryDhtCmd = &cmds.Command{ go func() { defer close(outChan) for e := range events { - outChan <- e + select { + case outChan <- e: + case <-req.Context().Done(): + return + } } }() }, @@ -181,7 +185,11 @@ var findProvidersDhtCmd = &cmds.Command{ go func() { defer close(outChan) for e := range events { - outChan <- e + select { + case outChan <- e: + case <-req.Context().Done(): + return + } } }() @@ -301,7 +309,11 @@ var provideRefDhtCmd = &cmds.Command{ go func() { defer close(outChan) for e := range events { - outChan <- e + select { + case outChan <- e: + case <-req.Context().Done(): + return + } } }() @@ -427,7 +439,11 @@ var findPeerDhtCmd = &cmds.Command{ go func() { defer close(outChan) for v := range events { - outChan <- v + select { + case outChan <- v: + case <-req.Context().Done(): + } + } }() @@ -529,7 +545,10 @@ Different key types can specify other 'best' rules. go func() { defer close(outChan) for e := range events { - outChan <- e + select { + case outChan <- e: + case <-req.Context().Done(): + } } }() @@ -643,7 +662,11 @@ NOTE: A value may not exceed 2048 bytes. go func() { defer close(outChan) for e := range events { - outChan <- e + select { + case outChan <- e: + case <-req.Context().Done(): + return + } } }()