Skip to content

Commit ff1674a

Browse files
authored
Merge pull request moby#5031 from tonistiigi/context-withoutcancel
ensure context.WithoutCancel in defer funcs
2 parents 12f04ef + 4103099 commit ff1674a

File tree

20 files changed

+56
-43
lines changed

20 files changed

+56
-43
lines changed

cache/blobs_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
4646

4747
defer func() {
4848
if cw != nil {
49+
ctx = context.WithoutCancel(ctx)
4950
// after commit success cw will be set to nil, if cw isn't nil, error
5051
// happened before commit, we should abort this ingest, and because the
5152
// error may incured by ctx cancel, use a new context here. And since
5253
// cm.Close will unlock this ref in the content store, we invoke abort
5354
// to remove the ingest root in advance.
54-
if aerr := sr.cm.ContentStore.Abort(context.Background(), ref); aerr != nil {
55+
if aerr := sr.cm.ContentStore.Abort(ctx, ref); aerr != nil {
5556
bklog.G(ctx).WithError(aerr).Warnf("failed to abort writer %q", ref)
5657
}
5758
if cerr := cw.Close(); cerr != nil {

cache/manager.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,
168168
releaseParent := false
169169
defer func() {
170170
if releaseParent || rerr != nil && p != nil {
171-
p.Release(context.TODO())
171+
p.Release(context.WithoutCancel(ctx))
172172
}
173173
}()
174174

@@ -242,7 +242,8 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,
242242

243243
defer func() {
244244
if rerr != nil {
245-
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
245+
ctx := context.WithoutCancel(ctx)
246+
if err := cm.LeaseManager.Delete(ctx, leases.Lease{
246247
ID: l.ID,
247248
}); err != nil {
248249
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)
@@ -423,7 +424,7 @@ func (cm *cacheManager) getRecord(ctx context.Context, id string, opts ...RefOpt
423424
}
424425
defer func() {
425426
if retErr != nil {
426-
parents.release(context.TODO())
427+
parents.release(context.WithoutCancel(ctx))
427428
}
428429
}()
429430

@@ -513,7 +514,7 @@ func (cm *cacheManager) getRecord(ctx context.Context, id string, opts ...RefOpt
513514
func (cm *cacheManager) parentsOf(ctx context.Context, md *cacheMetadata, opts ...RefOption) (ps parentRefs, rerr error) {
514515
defer func() {
515516
if rerr != nil {
516-
ps.release(context.TODO())
517+
ps.release(context.WithoutCancel(ctx))
517518
}
518519
}()
519520
if parentID := md.getParent(); parentID != "" {
@@ -580,7 +581,7 @@ func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, sess session.Gr
580581

581582
defer func() {
582583
if err != nil && parent != nil {
583-
parent.Release(context.TODO())
584+
parent.Release(context.WithoutCancel(ctx))
584585
}
585586
}()
586587

@@ -597,7 +598,8 @@ func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, sess session.Gr
597598

598599
defer func() {
599600
if err != nil {
600-
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
601+
ctx := context.WithoutCancel(ctx)
602+
if err := cm.LeaseManager.Delete(ctx, leases.Lease{
601603
ID: l.ID,
602604
}); err != nil {
603605
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)
@@ -705,7 +707,7 @@ func (cm *cacheManager) Merge(ctx context.Context, inputParents []ImmutableRef,
705707
dhs := make(map[digest.Digest]*DescHandler)
706708
defer func() {
707709
if rerr != nil {
708-
parents.release(context.TODO())
710+
parents.release(context.WithoutCancel(ctx))
709711
}
710712
}()
711713
for _, inputParent := range inputParents {
@@ -798,7 +800,8 @@ func (cm *cacheManager) createMergeRef(ctx context.Context, parents parentRefs,
798800
}
799801
defer func() {
800802
if rerr != nil {
801-
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
803+
ctx := context.WithoutCancel(ctx)
804+
if err := cm.LeaseManager.Delete(ctx, leases.Lease{
802805
ID: l.ID,
803806
}); err != nil {
804807
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)
@@ -834,7 +837,7 @@ func (cm *cacheManager) Diff(ctx context.Context, lower, upper ImmutableRef, pg
834837
dhs := make(map[digest.Digest]*DescHandler)
835838
defer func() {
836839
if rerr != nil {
837-
parents.release(context.TODO())
840+
parents.release(context.WithoutCancel(ctx))
838841
}
839842
}()
840843
for i, inputParent := range []ImmutableRef{lower, upper} {
@@ -889,7 +892,7 @@ func (cm *cacheManager) Diff(ctx context.Context, lower, upper ImmutableRef, pg
889892
mergeParents := parentRefs{mergeParents: make([]*immutableRef, len(upperLayers)-len(lowerLayers))}
890893
defer func() {
891894
if rerr != nil {
892-
mergeParents.release(context.TODO())
895+
mergeParents.release(context.WithoutCancel(ctx))
893896
}
894897
}()
895898
for i := len(lowerLayers); i < len(upperLayers); i++ {
@@ -954,7 +957,8 @@ func (cm *cacheManager) createDiffRef(ctx context.Context, parents parentRefs, d
954957
}
955958
defer func() {
956959
if rerr != nil {
957-
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
960+
ctx := context.WithoutCancel(ctx)
961+
if err := cm.LeaseManager.Delete(ctx, leases.Lease{
958962
ID: l.ID,
959963
}); err != nil {
960964
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)

cache/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ func TestLoopLeaseContent(t *testing.T) {
11681168
allRefs := []ImmutableRef{ref}
11691169
defer func() {
11701170
for _, ref := range allRefs {
1171-
ref.Release(ctx)
1171+
ref.Release(context.WithoutCancel(ctx))
11721172
}
11731173
}()
11741174
var chain []ocispecs.Descriptor

cache/refs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ func (sr *immutableRef) withRemoteSnapshotLabelsStargzMode(ctx context.Context,
10441044
return errors.Wrapf(err, "failed to add tmp remote labels for remote snapshot")
10451045
}
10461046
defer func() {
1047+
ctx := context.WithoutCancel(ctx)
10471048
for k := range info.Labels {
10481049
info.Labels[k] = "" // Remove labels appended in this call
10491050
}
@@ -1105,6 +1106,7 @@ func (sr *immutableRef) prepareRemoteSnapshotsStargzMode(ctx context.Context, s
11051106
info, err := r.cm.Snapshotter.Stat(ctx, snapshotID)
11061107
if err == nil { // usable as remote snapshot without unlazying.
11071108
defer func() {
1109+
ctx := context.WithoutCancel(ctx)
11081110
// Remove tmp labels appended in this func
11091111
if info.Labels != nil {
11101112
for k := range tmpLabels {

cache/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (sr *immutableRef) GetRemotes(ctx context.Context, createIfNeeded bool, ref
4040
if err != nil {
4141
return nil, err
4242
}
43-
defer done(ctx)
43+
defer done(context.WithoutCancel(ctx))
4444

4545
// fast path if compression variants aren't required
4646
// NOTE: compressionopt is applied only to *newly created layers* if Force != true.

client/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,13 @@ func testClientGatewayContainerExecPipe(t *testing.T, sb integration.Sandbox) {
432432
Args: []string{"sleep", "10"},
433433
})
434434
if err != nil {
435-
ctr.Release(ctx)
435+
ctr.Release(context.WithoutCancel(ctx))
436436
return nil, err
437437
}
438438

439439
defer func() {
440440
// cancel pid1
441-
ctr.Release(ctx)
441+
ctr.Release(context.WithoutCancel(ctx))
442442
pid1.Wait()
443443
}()
444444

executor/containerdexecutor/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M
166166
}
167167

168168
defer func() {
169-
if err1 := container.Delete(context.TODO()); err == nil && err1 != nil {
169+
if err1 := container.Delete(context.WithoutCancel(ctx)); err == nil && err1 != nil {
170170
err = errors.Wrapf(err1, "failed to delete container %s", id)
171171
}
172172
}()
@@ -190,7 +190,7 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M
190190
}
191191

192192
defer func() {
193-
if _, err1 := task.Delete(context.TODO(), containerd.WithProcessKill); err == nil && err1 != nil {
193+
if _, err1 := task.Delete(context.WithoutCancel(ctx), containerd.WithProcessKill); err == nil && err1 != nil {
194194
err = errors.Wrapf(err1, "failed to delete task %s", id)
195195
}
196196
}()

executor/runcexecutor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
237237
}
238238
defer mount.Unmount(rootFSPath, 0)
239239

240-
defer executor.MountStubsCleaner(ctx, rootFSPath, mounts, meta.RemoveMountStubsRecursive)()
240+
defer executor.MountStubsCleaner(context.WithoutCancel(ctx), rootFSPath, mounts, meta.RemoveMountStubsRecursive)()
241241

242242
uid, gid, sgids, err := oci.GetUser(rootFSPath, meta.User)
243243
if err != nil {

exporter/containerimage/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
226226
}
227227
defer func() {
228228
if descref == nil {
229-
done(context.TODO())
229+
done(context.WithoutCancel(ctx))
230230
}
231231
}()
232232

exporter/oci/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
155155
}
156156
defer func() {
157157
if descref == nil {
158-
done(context.TODO())
158+
done(context.WithoutCancel(ctx))
159159
}
160160
}()
161161

0 commit comments

Comments
 (0)