Skip to content

Commit 70ab3ca

Browse files
committed
replace context.WithCancel with WithCancelCause
Keep stack traces for cancellation errors where possible. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 06c971f commit 70ab3ca

File tree

46 files changed

+187
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+187
-165
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ linters-settings:
4141
forbid:
4242
- '^fmt\.Errorf(# use errors\.Errorf instead)?$'
4343
- '^logrus\.(Trace|Debug|Info|Warn|Warning|Error|Fatal)(f|ln)?(# use bklog\.G or bklog\.L instead of logrus directly)?$'
44+
- '^context\.WithCancel(# use context\.WithCancelCause instead)?$'
45+
- '^ctx\.Err(# use context\.Cause instead)?$'
4446
importas:
4547
alias:
4648
- pkg: "github.com/opencontainers/image-spec/specs-go/v1"

cache/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt
12581258

12591259
select {
12601260
case <-ctx.Done():
1261-
return ctx.Err()
1261+
return context.Cause(ctx)
12621262
default:
12631263
return cm.prune(ctx, ch, opt)
12641264
}

client/build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,8 +1266,8 @@ func testClientGatewayContainerCancelExecTty(t *testing.T, sb integration.Sandbo
12661266
defer pid1.Wait()
12671267
defer ctr.Release(ctx)
12681268

1269-
execCtx, cancel := context.WithCancel(ctx)
1270-
defer cancel()
1269+
execCtx, cancel := context.WithCancelCause(ctx)
1270+
defer cancel(errors.WithStack(context.Canceled))
12711271

12721272
prompt := newTestPrompt(execCtx, t, inputW, output)
12731273
pid2, err := ctr.Start(execCtx, client.StartRequest{
@@ -1281,7 +1281,7 @@ func testClientGatewayContainerCancelExecTty(t *testing.T, sb integration.Sandbo
12811281
require.NoError(t, err)
12821282

12831283
prompt.SendExpect("echo hi", "hi")
1284-
cancel()
1284+
cancel(errors.WithStack(context.Canceled))
12851285

12861286
err = pid2.Wait()
12871287
require.ErrorIs(t, err, context.Canceled)

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (c *Client) Wait(ctx context.Context) error {
205205

206206
select {
207207
case <-ctx.Done():
208-
return ctx.Err()
208+
return context.Cause(ctx)
209209
case <-time.After(time.Second):
210210
}
211211
c.conn.ResetConnectBackoff()

client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7407,8 +7407,8 @@ func testInvalidExporter(t *testing.T, sb integration.Sandbox) {
74077407

74087408
// moby/buildkit#492
74097409
func testParallelLocalBuilds(t *testing.T, sb integration.Sandbox) {
7410-
ctx, cancel := context.WithCancel(sb.Context())
7411-
defer cancel()
7410+
ctx, cancel := context.WithCancelCause(sb.Context())
7411+
defer cancel(errors.WithStack(context.Canceled))
74127412

74137413
c, err := New(ctx, sb.Address())
74147414
require.NoError(t, err)

client/llb/async.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (as *asyncState) Do(ctx context.Context, c *Constraints) error {
6161
if err != nil {
6262
select {
6363
case <-ctx.Done():
64-
if errors.Is(err, ctx.Err()) {
64+
if errors.Is(err, context.Cause(ctx)) {
6565
return res, err
6666
}
6767
default:

client/solve.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
106106
}
107107
eg, ctx := errgroup.WithContext(ctx)
108108

109-
statusContext, cancelStatus := context.WithCancel(context.Background())
110-
defer cancelStatus()
109+
statusContext, cancelStatus := context.WithCancelCause(context.Background())
110+
defer cancelStatus(errors.WithStack(context.Canceled))
111111

112112
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
113113
statusContext = trace.ContextWithSpan(statusContext, span)
@@ -230,16 +230,16 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
230230
frontendAttrs[k] = v
231231
}
232232

233-
solveCtx, cancelSolve := context.WithCancel(ctx)
233+
solveCtx, cancelSolve := context.WithCancelCause(ctx)
234234
var res *SolveResponse
235235
eg.Go(func() error {
236236
ctx := solveCtx
237-
defer cancelSolve()
237+
defer cancelSolve(errors.WithStack(context.Canceled))
238238

239239
defer func() { // make sure the Status ends cleanly on build errors
240240
go func() {
241241
<-time.After(3 * time.Second)
242-
cancelStatus()
242+
cancelStatus(errors.WithStack(context.Canceled))
243243
}()
244244
if !opt.SessionPreInitialized {
245245
bklog.G(ctx).Debugf("stopping session")
@@ -298,7 +298,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
298298
select {
299299
case <-solveCtx.Done():
300300
case <-time.After(5 * time.Second):
301-
cancelSolve()
301+
cancelSolve(errors.WithStack(context.Canceled))
302302
}
303303

304304
return err

cmd/buildkitd/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func main() {
223223
if os.Geteuid() > 0 {
224224
return errors.New("rootless mode requires to be executed as the mapped root in a user namespace; you may use RootlessKit for setting up the namespace")
225225
}
226-
ctx, cancel := context.WithCancel(appcontext.Context())
227-
defer cancel()
226+
ctx, cancel := context.WithCancelCause(appcontext.Context())
227+
defer cancel(errors.WithStack(context.Canceled))
228228

229229
cfg, err := config.LoadFile(c.GlobalString("config"))
230230
if err != nil {
@@ -344,9 +344,9 @@ func main() {
344344
select {
345345
case serverErr := <-errCh:
346346
err = serverErr
347-
cancel()
347+
cancel(err)
348348
case <-ctx.Done():
349-
err = ctx.Err()
349+
err = context.Cause(ctx)
350350
}
351351

352352
bklog.G(ctx).Infof("stopping server")
@@ -634,14 +634,14 @@ func unaryInterceptor(globalCtx context.Context, tp trace.TracerProvider) grpc.U
634634
withTrace := otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(tp), otelgrpc.WithPropagators(propagators))
635635

636636
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
637-
ctx, cancel := context.WithCancel(ctx)
638-
defer cancel()
637+
ctx, cancel := context.WithCancelCause(ctx)
638+
defer cancel(errors.WithStack(context.Canceled))
639639

640640
go func() {
641641
select {
642642
case <-ctx.Done():
643643
case <-globalCtx.Done():
644-
cancel()
644+
cancel(context.Cause(globalCtx))
645645
}
646646
}()
647647

control/control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ func (c *Controller) Session(stream controlapi.Control_SessionServer) error {
505505
conn, closeCh, opts := grpchijack.Hijack(stream)
506506
defer conn.Close()
507507

508-
ctx, cancel := context.WithCancel(stream.Context())
508+
ctx, cancel := context.WithCancelCause(stream.Context())
509509
go func() {
510510
<-closeCh
511-
cancel()
511+
cancel(errors.WithStack(context.Canceled))
512512
}()
513513

514514
err := c.opt.SessionManager.HandleConn(ctx, conn, opts)

executor/containerdexecutor/executor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (w *containerdExecutor) Exec(ctx context.Context, id string, process execut
243243
}
244244
select {
245245
case <-ctx.Done():
246-
return ctx.Err()
246+
return context.Cause(ctx)
247247
case err, ok := <-details.done:
248248
if !ok || err == nil {
249249
return errors.Errorf("container %s has stopped", id)
@@ -336,8 +336,8 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
336336

337337
// handle signals (and resize) in separate go loop so it does not
338338
// potentially block the container cancel/exit status loop below.
339-
eventCtx, eventCancel := context.WithCancel(ctx)
340-
defer eventCancel()
339+
eventCtx, eventCancel := context.WithCancelCause(ctx)
340+
defer eventCancel(errors.WithStack(context.Canceled))
341341
go func() {
342342
for {
343343
select {
@@ -403,7 +403,7 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
403403
}
404404
select {
405405
case <-ctx.Done():
406-
exitErr.Err = errors.Wrap(ctx.Err(), exitErr.Error())
406+
exitErr.Err = errors.Wrap(context.Cause(ctx), exitErr.Error())
407407
default:
408408
}
409409
return exitErr

0 commit comments

Comments
 (0)