From 4ab28ee12cc49cf444dbf35957f09e35fcb83818 Mon Sep 17 00:00:00 2001 From: Edoardo Spadolini Date: Thu, 6 Oct 2022 17:27:58 +0200 Subject: [PATCH] Drain errChan in `api.client/connect` This fixes a goroutine leak caused by the various connect methods getting stuck waiting to push their error in `errChan` after the function exited with a success. --- api/client/client.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/api/client/client.go b/api/client/client.go index 0695f454f98c4..ec819165ff86a 100644 --- a/api/client/client.go +++ b/api/client/client.go @@ -262,18 +262,22 @@ func connect(ctx context.Context, cfg Config) (*Client, error) { }() var errs []error - for errChan != nil { +Outer: + for { select { // Use the first client to successfully connect in syncConnect. case clt := <-cltChan: + go func() { + for range errChan { + } + }() return clt, nil case err, ok := <-errChan: - if ok { - // Add a new line to make errs human readable. - errs = append(errs, trace.Wrap(err, "")) - continue + if !ok { + break Outer } - errChan = nil + // Add a new line to make errs human readable. + errs = append(errs, trace.Wrap(err, "")) } }