Skip to content

Commit

Permalink
Pass errors up the stack in CalculateWorld and InstallPackages (#1404)
Browse files Browse the repository at this point in the history
Make sure to close channel in InstallPackages, cleanup in CalculateWorld
    
This problem was found in melange
chainguard-dev/melange#1645
    
Any time a download failed, we would hang waiting for a close
that would never occur.

Signed-off-by: Scott Moser <[email protected]>
  • Loading branch information
smoser authored Nov 14, 2024
1 parent b93f0a2 commit 87846cb
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pkg/apk/apk/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,7 @@ 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))
for i := range allpkgs {
done[i] = make(chan struct{})
}

// Meanwhile, concurrently fetch and expand all our APKs.
// We signal they are ready to be installed by closing done[i].
// concurrently fetch and expand all our APKs.
for i, pkg := range allpkgs {
i, pkg := i, pkg

Expand All @@ -547,8 +540,6 @@ func (a *APK) CalculateWorld(ctx context.Context, allpkgs []*RepositoryPackage)
res.Package = pkg
resolved[i] = res

close(done[i])

return nil
})
}
Expand Down Expand Up @@ -664,6 +655,10 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
exp := expanded[i]
pkg := allpkgs[i]

if exp == nil {
return fmt.Errorf("expansion of %s failed", pkg)
}

isInstalled, err := a.isInstalledPackage(pkg.PackageName())
if err != nil {
return fmt.Errorf("error checking if package %s is installed: %w", pkg, err)
Expand Down Expand Up @@ -698,13 +693,13 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
i, pkg := i, pkg

g.Go(func() error {
defer func() { close(done[i]) }()
exp, err := a.expandPackage(ctx, pkg)
if err != nil {
return fmt.Errorf("expanding %s: %w", pkg, err)
}

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

return nil
})
Expand Down

0 comments on commit 87846cb

Please sign in to comment.