Skip to content

Commit 09b184c

Browse files
committed
rootfs: use new ctx to cleanup instead of canceled one
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]>
1 parent 4a2f61c commit 09b184c

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

rootfs/diff.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/containerd/containerd/diff"
2424
"github.com/containerd/containerd/mount"
25+
"github.com/containerd/containerd/namespaces"
2526
"github.com/containerd/containerd/snapshots"
2627
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2728
)
@@ -31,6 +32,13 @@ import (
3132
// the content creation and the provided snapshotter and mount differ are used
3233
// for calculating the diff. The descriptor for the layer diff is returned.
3334
func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter, d diff.Comparer, opts ...diff.Opt) (ocispec.Descriptor, error) {
35+
// dctx is used to handle cleanup things just in case the param ctx
36+
// has been canceled, which causes that the defer cleanup fails.
37+
dctx := context.Background()
38+
if ns, ok := namespaces.Namespace(ctx); ok {
39+
dctx = namespaces.WithNamespace(dctx, ns)
40+
}
41+
3442
info, err := sn.Stat(ctx, snapshotID)
3543
if err != nil {
3644
return ocispec.Descriptor{}, err
@@ -41,7 +49,7 @@ func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter
4149
if err != nil {
4250
return ocispec.Descriptor{}, err
4351
}
44-
defer sn.Remove(ctx, lowerKey)
52+
defer sn.Remove(dctx, lowerKey)
4553

4654
var upper []mount.Mount
4755
if info.Kind == snapshots.KindActive {
@@ -55,7 +63,7 @@ func CreateDiff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter
5563
if err != nil {
5664
return ocispec.Descriptor{}, err
5765
}
58-
defer sn.Remove(ctx, upperKey)
66+
defer sn.Remove(dctx, upperKey)
5967
}
6068

6169
return d.Compare(ctx, lower, upper, opts...)

0 commit comments

Comments
 (0)