From 39ec4a0a2f96651e85e10d30283ae4b20341a578 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Fri, 20 Aug 2021 18:39:12 +0200 Subject: [PATCH] Fix waiting group in client runner Signed-off-by: Tom Chauveau --- client/client.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/client.go b/client/client.go index b75d325c..4709fa98 100644 --- a/client/client.go +++ b/client/client.go @@ -104,8 +104,14 @@ func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) error { } func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.Environment, fn DoFunc, ch chan *bk.SolveStatus) error { + wg := sync.WaitGroup{} + // Close output channel - defer close(ch) + defer func() { + // Wait until all the events are caught + wg.Wait() + close(ch) + }() lg := log.Ctx(ctx) @@ -143,7 +149,6 @@ func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment. Interface("attrs", opts.FrontendAttrs). Msg("spawning buildkit job") - wg := sync.WaitGroup{} // Catch output from events catchOutput := func(inCh chan *bk.SolveStatus) { for e := range inCh { @@ -220,8 +225,6 @@ func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment. Msg("exporter response") } - // Wait until all the events are caught - wg.Wait() return nil }