Skip to content

Commit

Permalink
Pass errors up the stack in CalculateWorld and InstallPackages
Browse files Browse the repository at this point in the history
This problem was found in melange
 chainguard-dev/melange#1645

Any time a download failed, we did not communicate the error
up the stack.

Signed-off-by: Scott Moser <[email protected]>
  • Loading branch information
smoser committed Nov 14, 2024
1 parent 213dc9b commit d5d4236
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pkg/apk/apk/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,9 @@ func (a *APK) CalculateWorld(ctx context.Context, allpkgs []*RepositoryPackage)
resolved := make([]*APKResolved, len(allpkgs))

// A slice of pseudo-promises that get closed when expanded[i] is ready.
done := make([]chan struct{}, len(allpkgs))
done := make([]chan error, len(allpkgs))
for i := range allpkgs {
done[i] = make(chan struct{})
done[i] = make(chan error)
}

// Meanwhile, concurrently fetch and expand all our APKs.
Expand All @@ -537,18 +537,22 @@ func (a *APK) CalculateWorld(ctx context.Context, allpkgs []*RepositoryPackage)
g.Go(func() error {
r, err := a.FetchPackage(ctx, pkg)
if err != nil {
return fmt.Errorf("fetching %s: %w", pkg.Name, err)
err = fmt.Errorf("fetching %s: %w", pkg.Name, err)
done[i] <- err
return err
}

res, err := ResolveApk(ctx, r)
if err != nil {
return fmt.Errorf("resolving %s: %w", pkg.Name, err)
err = fmt.Errorf("resolving %s: %w", pkg.Name, err)
done[i] <- err
return err
}

res.Package = pkg
resolved[i] = res

close(done[i])

done[i] <- nil
return nil
})
}
Expand Down Expand Up @@ -645,9 +649,9 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
infos := make([]*Package, len(allpkgs))

// A slice of pseudo-promises that get closed when expanded[i] is ready.
done := make([]chan struct{}, len(allpkgs))
done := make([]chan error, len(allpkgs))
for i := range allpkgs {
done[i] = make(chan struct{})
done[i] = make(chan error)
}

// Kick off a goroutine that sequentially installs packages as they become ready.
Expand All @@ -660,7 +664,10 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
select {
case <-ctx.Done():
return ctx.Err()
case <-ch:
case err := <-ch:
if err != nil {
return err
}
exp := expanded[i]
pkg := allpkgs[i]

Expand Down Expand Up @@ -700,13 +707,13 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
g.Go(func() error {
exp, err := a.expandPackage(ctx, pkg)
if err != nil {
return fmt.Errorf("expanding %s: %w", pkg, err)
err = fmt.Errorf("expanding %s: %w", pkg, err)
} else {
expanded[i] = exp
}

expanded[i] = exp
close(done[i])

return nil
done[i] <- err
return err
})
}

Expand Down

0 comments on commit d5d4236

Please sign in to comment.