From d4e883841dd467f13aa5169a4d993eccf529e8eb Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 20 Nov 2017 19:44:51 -0800 Subject: [PATCH] fix concurrent SetError in add command I believe this also fixes a potential go routine leak (on race). License: MIT Signed-off-by: Steven Allen --- core/commands/add.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index d5429168735e..f23afd4ab4bc 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -342,6 +342,8 @@ You can now check what blocks have been created by: }, PostRun: map[cmds.EncodingType]func(cmds.Request, cmds.ResponseEmitter) cmds.ResponseEmitter{ cmds.CLI: func(req cmds.Request, re cmds.ResponseEmitter) cmds.ResponseEmitter { + ctx := req.Context() + reNext, res := cmds.NewChanResponsePair(req) outChan := make(chan interface{}) @@ -429,9 +431,6 @@ You can now check what blocks have been created by: bar.ShowBar = true bar.ShowTimeLeft = true } - case <-req.Context().Done(): - re.SetError(req.Context().Err(), cmdkit.ErrNormal) - return } } } @@ -469,7 +468,12 @@ You can now check what blocks have been created by: return } - outChan <- v + select { + case outChan <- v: + case <-ctx.Done(): + re.SetError(ctx.Err(), cmdkit.ErrNormal) + return + } } }()