Skip to content

Commit

Permalink
Fix dead-lock in checkoutSources
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Nov 29, 2020
1 parent 4ace6bf commit e87c78a
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions pkg/app/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
Expand All @@ -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
Expand Down

0 comments on commit e87c78a

Please sign in to comment.