From e87c78ad6b30dd29ed9363051452bb9c455ae534 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 29 Nov 2020 14:27:28 +0900 Subject: [PATCH] Fix dead-lock in checkoutSources --- pkg/app/source.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/app/source.go b/pkg/app/source.go index bb753a5..7d32987 100644 --- a/pkg/app/source.go +++ b/pkg/app/source.go @@ -10,40 +10,44 @@ import ( ) func (app *App) checkoutSources(_ *EventLogger, jobCtx *JobContext, sources []Source, concurrency int) error { + if len(sources) == 0 { + return nil + } + type result struct { id string dir string } - inputs := make(chan Source) + sourceCh := make(chan Source) - results := make(chan result) + resultCh := make(chan result) - eg, ctx := errgroup.WithContext(context.Background()) + sourceWorkers, ctx := errgroup.WithContext(context.Background()) for i := 0; i < concurrency; i++ { - eg.Go(func() error { - for src := range inputs { + sourceWorkers.Go(func() error { + for src := range sourceCh { dir, err := app.sourceClient.ExtractSource(ctx, src.Kind, src.Namepsace, src.Name) if err != nil { return xerrors.Errorf("extracting source: %w", err) } - results <- result{id: src.ID, dir: dir} + resultCh <- result{id: src.ID, dir: dir} } return nil }) } - srcs := map[string]cty.Value{} + results := map[string]cty.Value{} wg := &sync.WaitGroup{} wg.Add(1) go func() { - for src := range results { - srcs[src.id] = cty.MapVal(map[string]cty.Value{ + for src := range resultCh { + results[src.id] = cty.MapVal(map[string]cty.Value{ "dir": cty.StringVal(src.dir), }) } @@ -52,18 +56,22 @@ func (app *App) checkoutSources(_ *EventLogger, jobCtx *JobContext, sources []So }() for _, src := range sources { - inputs <- src + sourceCh <- src } - if err := eg.Wait(); err != nil { + close(sourceCh) + + if err := sourceWorkers.Wait(); err != nil { return xerrors.Errorf("waiting for workers: %w", err) } + close(resultCh) + wg.Wait() - if len(srcs) > 0 { + if len(results) > 0 { // Cuz calling cty.MapVal on an empty map panics by its nature - jobCtx.evalContext.Variables["source"] = cty.MapVal(srcs) + jobCtx.evalContext.Variables["source"] = cty.MapVal(results) } return nil