From 0b98f33d2c2339e5a3ef7013b743aafd279eb8b5 Mon Sep 17 00:00:00 2001 From: ashnamehrotra Date: Tue, 20 Feb 2024 10:52:32 -0800 Subject: [PATCH] fix: Copa hangs when InstallUpdates fails (#508) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ashnamehrotra Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com> --- pkg/patch/patch.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/patch/patch.go b/pkg/patch/patch.go index 799b697f..c67203de 100644 --- a/pkg/patch/patch.go +++ b/pkg/patch/patch.go @@ -45,7 +45,7 @@ func Patch(ctx context.Context, timeout time.Duration, image, reportFile, patche ch := make(chan error) go func() { - ch <- patchWithContext(timeoutCtx, image, reportFile, patchedTag, workingFolder, scanner, format, output, ignoreError, bkOpts) + ch <- patchWithContext(timeoutCtx, ch, image, reportFile, patchedTag, workingFolder, scanner, format, output, ignoreError, bkOpts) }() select { @@ -70,7 +70,7 @@ func removeIfNotDebug(workingFolder string) { } } -func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error { +func patchWithContext(ctx context.Context, ch chan error, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error { imageName, err := reference.ParseNormalizedNamed(image) if err != nil { return err @@ -81,7 +81,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin } taggedName, ok := imageName.(reference.Tagged) if !ok { - err := errors.New("unexpected: TagNameOnly did create Tagged ref") + err := errors.New("unexpected: TagNameOnly did not create Tagged ref") log.Error(err) return err } @@ -156,19 +156,21 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin return err } - ch := make(chan *client.SolveStatus) + buildChannel := make(chan *client.SolveStatus) eg, ctx := errgroup.WithContext(ctx) eg.Go(func() error { _, err := bkClient.Build(ctx, solveOpt, copaProduct, func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) { // Configure buildctl/client for use by package manager config, err := buildkit.InitializeBuildkitConfig(ctx, c, imageName.String(), updates) if err != nil { + ch <- err return nil, err } // Create package manager helper pkgmgr, err := pkgmgr.GetPackageManager(updates.Metadata.OS.Type, config, workingFolder) if err != nil { + ch <- err return nil, err } @@ -176,11 +178,13 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin // TODO: Add support for other output modes as buildctl does. patchedImageState, errPkgs, err := pkgmgr.InstallUpdates(ctx, updates, ignoreError) if err != nil { + ch <- err return nil, err } def, err := patchedImageState.Marshal(ctx) if err != nil { + ch <- err return nil, err } @@ -191,6 +195,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin res.AddMeta(exptypes.ExporterImageConfigKey, config.ConfigData) if err != nil { + ch <- err return nil, err } @@ -215,12 +220,13 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin // vex document must contain at least one statement if output != "" && len(validatedManifest.Updates) > 0 { if err := vex.TryOutputVexDocument(validatedManifest, pkgmgr, patchedImageName, format, output); err != nil { + ch <- err return nil, err } } return res, nil - }, ch) + }, buildChannel) return err }) @@ -231,7 +237,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin c = cn } // not using shared context to not disrupt display but let us finish reporting errors - _, err = progressui.DisplaySolveStatus(context.TODO(), c, os.Stdout, ch) + _, err = progressui.DisplaySolveStatus(context.TODO(), c, os.Stdout, buildChannel) return err })