Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overlay differ: cancel diff calculation cleanly #2662

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cache/blobs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/overlay"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -41,8 +42,10 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
return emptyDesc, false, errors.Wrap(err, "failed to open writer")
}
defer func() {
if err != nil {
cw.Close()
if cw != nil {
if cerr := cw.Close(); cerr != nil {
bklog.G(ctx).WithError(cerr).Warnf("failed to close writer %q", ref)
}
}
}()

Expand Down Expand Up @@ -82,6 +85,10 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
return emptyDesc, false, errors.Wrap(err, "failed to commit")
}
}
if err := cw.Close(); err != nil {
return emptyDesc, false, err
}
cw = nil
cinfo, err := sr.cm.ContentStore.Info(ctx, dgst)
if err != nil {
return emptyDesc, false, errors.Wrap(err, "failed to get info from content store")
Expand Down
21 changes: 18 additions & 3 deletions util/overlay/overlay_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,30 @@ func WriteUpperdir(ctx context.Context, w io.Writer, upperdir string, lower []mo
}
return mount.WithTempMount(ctx, lower, func(lowerRoot string) error {
return mount.WithTempMount(ctx, upperView, func(upperViewRoot string) error {
cw := archive.NewChangeWriter(w, upperViewRoot)
cw := archive.NewChangeWriter(&cancellableWriter{ctx, w}, upperViewRoot)
if err := Changes(ctx, cw.HandleChange, upperdir, upperViewRoot, lowerRoot); err != nil {
if err2 := cw.Close(); err2 != nil {
return errors.Wrapf(err, "failed torecord upperdir changes (close error: %v)", err2)
return errors.Wrapf(err, "failed to record upperdir changes (close error: %v)", err2)
}
return errors.Wrapf(err, "failed torecord upperdir changes")
return errors.Wrapf(err, "failed to record upperdir changes")
}
return cw.Close()
})
})
}

type cancellableWriter struct {
ctx context.Context
w io.Writer
}

func (w *cancellableWriter) Write(p []byte) (int, error) {
if err := w.ctx.Err(); err != nil {
return 0, err
}
return w.w.Write(p)
}

// Changes is continuty's `fs.Change`-like method but leverages overlayfs's
// "upperdir" for computing the diff. "upperdirView" is overlayfs mounted view of
// the upperdir that doesn't contain whiteouts. This is used for computing
Expand All @@ -149,6 +161,9 @@ func Changes(ctx context.Context, changeFn fs.ChangeFunc, upperdir, upperdirView
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}

// Rebase path
path, err = filepath.Rel(upperdir, path)
Expand Down