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 a5bbb7f
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 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 Expand Up @@ -1151,7 +1158,8 @@ func (a *APK) expandPackage(ctx context.Context, pkg InstallablePackage) (*expan
return expandPackage(ctx, a, pkg)
}

return globalApkCache.get(ctx, a, pkg)
r, err := globalApkCache.get(ctx, a, pkg)
return r, err
}

func expandPackage(ctx context.Context, a *APK, pkg InstallablePackage) (*expandapk.APKExpanded, error) {
Expand Down

0 comments on commit a5bbb7f

Please sign in to comment.