Skip to content

Commit

Permalink
rootfs: use new ctx to cleanup instead of canceled one
Browse files Browse the repository at this point in the history
rootfs.CreateDiff might be canceled by context for some reason. Based on
this case, the defer function should use the new ctx to do cleanup
temporary snapshotter instead of the canceled one.

Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Aug 27, 2019
1 parent 4a2f61c commit 09b184c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rootfs/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/snapshots"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -31,6 +32,13 @@ import (
// the content creation and the provided snapshotter and mount differ are used
// for calculating the diff. The descriptor for the layer diff is returned.
func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter, d diff.Comparer, opts ...diff.Opt) (ocispec.Descriptor, error) {
// dctx is used to handle cleanup things just in case the param ctx
// has been canceled, which causes that the defer cleanup fails.
dctx := context.Background()
if ns, ok := namespaces.Namespace(ctx); ok {
dctx = namespaces.WithNamespace(dctx, ns)
}

info, err := sn.Stat(ctx, snapshotID)
if err != nil {
return ocispec.Descriptor{}, err
Expand All @@ -41,7 +49,7 @@ func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter
if err != nil {
return ocispec.Descriptor{}, err
}
defer sn.Remove(ctx, lowerKey)
defer sn.Remove(dctx, lowerKey)

var upper []mount.Mount
if info.Kind == snapshots.KindActive {
Expand All @@ -55,7 +63,7 @@ func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter
if err != nil {
return ocispec.Descriptor{}, err
}
defer sn.Remove(ctx, upperKey)
defer sn.Remove(dctx, upperKey)
}

return d.Compare(ctx, lower, upper, opts...)
Expand Down

0 comments on commit 09b184c

Please sign in to comment.