Skip to content

Commit

Permalink
Handle job errors by exiting early in DirImage.Write
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekjarosik committed Nov 4, 2024
1 parent cd1b2fc commit bdf2c7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pkg/dirimage/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (di *DirImage) Write(ctx context.Context, destinationDir string, opt ...Opt
}

jobs := make(chan Job, opts.workersCount)
g, ctx := errgroup.WithContext(ctx)
g, groupCtx := errgroup.WithContext(ctx)
layerOpts := []filesegment.LayerOpt{filesegment.WithLogFunction(opts.printf)}
for w := 0; w < opts.workersCount; w++ {
g.Go(func() error {
Expand Down Expand Up @@ -147,8 +147,8 @@ func (di *DirImage) Write(ctx context.Context, destinationDir string, opt ...Opt
return err
}
select {
case <-ctx.Done():
return ctx.Err() // Early return on context cancellation.
case <-groupCtx.Done():
return groupCtx.Err() // Early return on context cancellation.
case jobs <- Job{Descriptor: *d, Layer: l}:
}
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/dirimage/write_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package dirimage

import (
"context"
"errors"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/macvmio/geranos/pkg/filesegment"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
)

/*
func TestWrite_ContextCancelledDuringWork(t *testing.T) {

tempDir, err := os.MkdirTemp("", "write-test-*")
Expand All @@ -26,24 +31,22 @@ func TestWrite_ContextCancelledDuringWork(t *testing.T) {
img, err = mutate.Append(img, mutate.Addendum{
Layer: layer,
Annotations: layer.Annotations(),
MediaType: layer.GetMediaType(),
MediaType: "",
})
require.NoError(t, err)
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

di := DirImage{
Image: img,
}
di, err := Convert(img)
require.NoError(t, err)
err = di.Write(ctx, filepath.Join(tempDir, "testdir1"), WithWorkersCount(2))

if err == nil || !errors.Is(err, context.Canceled) {
t.Errorf("Write did not return expected context.Canceled error during work, got: %v", err)
}
}
*/

func TestDirImage_deleteManifest(t *testing.T) {
// Create a temporary directory for testing
Expand Down

0 comments on commit bdf2c7b

Please sign in to comment.