Skip to content

Commit e05a89e

Browse files
committed
improve stacks of cancels from defers
In this case the current stack trace points to the line where the context was created. Instead the stack should be captured when the defer is running so the return path to the defer call is also part of the stack. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent ab83f87 commit e05a89e

File tree

25 files changed

+28
-28
lines changed

25 files changed

+28
-28
lines changed

cache/remotecache/local/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func getContentStore(ctx context.Context, sm *session.Manager, g session.Group,
107107
}
108108
timeoutCtx, cancel := context.WithCancelCause(ctx)
109109
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded))
110-
defer cancel(errors.WithStack(context.Canceled))
110+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
111111

112112
caller, err := sm.Get(timeoutCtx, sessionID, false)
113113
if err != nil {

client/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ func testClientGatewayContainerCancelExecTty(t *testing.T, sb integration.Sandbo
12361236
defer ctr.Release(ctx)
12371237

12381238
execCtx, cancel := context.WithCancelCause(ctx)
1239-
defer cancel(errors.WithStack(context.Canceled))
1239+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
12401240

12411241
prompt := newTestPrompt(execCtx, t, inputW, output)
12421242
pid2, err := ctr.Start(execCtx, client.StartRequest{

client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8018,7 +8018,7 @@ func testInvalidExporter(t *testing.T, sb integration.Sandbox) {
80188018
func testParallelLocalBuilds(t *testing.T, sb integration.Sandbox) {
80198019
integration.SkipOnPlatform(t, "windows")
80208020
ctx, cancel := context.WithCancelCause(sb.Context())
8021-
defer cancel(errors.WithStack(context.Canceled))
8021+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
80228022

80238023
c, err := New(ctx, sb.Address())
80248024
require.NoError(t, err)

cmd/buildctl/common/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func ResolveClient(c *cli.Context) (*client.Client, error) {
8686
ctx2, cancel := context.WithCancelCause(ctx)
8787
ctx2, _ = context.WithTimeoutCause(ctx2, timeout*time.Second, errors.WithStack(context.DeadlineExceeded))
8888
ctx = ctx2
89-
defer cancel(errors.WithStack(context.Canceled))
89+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
9090
}
9191

9292
cl, err := client.New(ctx, c.GlobalString("addr"), opts...)

cmd/buildkitd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func main() {
233233
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")
234234
}
235235
ctx, cancel := context.WithCancelCause(appcontext.Context())
236-
defer cancel(errors.WithStack(context.Canceled))
236+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
237237

238238
cfg, err := config.LoadFile(c.GlobalString("config"))
239239
if err != nil {

control/gateway/gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (gwf *GatewayForwarder) lookupForwarder(ctx context.Context) (gateway.LLBBr
6161

6262
ctx, cancel := context.WithCancelCause(ctx)
6363
ctx, _ = context.WithTimeoutCause(ctx, 3*time.Second, errors.WithStack(context.DeadlineExceeded))
64-
defer cancel(errors.WithStack(context.Canceled))
64+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
6565

6666
go func() {
6767
<-ctx.Done()

executor/runcexecutor/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func (k procKiller) Kill(ctx context.Context) (err error) {
547547
// shorter timeout but here as a fail-safe for future refactoring.
548548
ctx, cancel := context.WithCancelCause(ctx)
549549
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, errors.WithStack(context.DeadlineExceeded))
550-
defer cancel(errors.WithStack(context.Canceled))
550+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
551551

552552
if k.pidfile == "" {
553553
// for `runc run` process we use `runc kill` to terminate the process
@@ -694,7 +694,7 @@ func (p *procHandle) WaitForReady(ctx context.Context) error {
694694
func (p *procHandle) WaitForStart(ctx context.Context, startedCh <-chan int, started func()) error {
695695
ctx, cancel := context.WithCancelCause(ctx)
696696
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, errors.WithStack(context.DeadlineExceeded))
697-
defer cancel(errors.WithStack(context.Canceled))
697+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
698698
select {
699699
case <-ctx.Done():
700700
return errors.New("go-runc started message never received")

exporter/local/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (e *localExporter) Config() *exporter.Config {
8181
func (e *localExporterInstance) Export(ctx context.Context, inp *exporter.Source, _ exptypes.InlineCache, sessionID string) (map[string]string, exporter.DescriptorReference, error) {
8282
timeoutCtx, cancel := context.WithCancelCause(ctx)
8383
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded))
84-
defer cancel(errors.WithStack(context.Canceled))
84+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
8585

8686
if e.opts.Epoch == nil {
8787
if tm, ok, err := epoch.ParseSource(inp); err != nil {

exporter/oci/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
218218

219219
timeoutCtx, cancel := context.WithCancelCause(ctx)
220220
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded))
221-
defer cancel(errors.WithStack(context.Canceled))
221+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
222222

223223
caller, err := e.opt.SessionManager.Get(timeoutCtx, sessionID, false)
224224
if err != nil {

exporter/tar/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (e *localExporterInstance) Export(ctx context.Context, inp *exporter.Source
165165

166166
timeoutCtx, cancel := context.WithCancelCause(ctx)
167167
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded))
168-
defer cancel(errors.WithStack(context.Canceled))
168+
defer func() { cancel(errors.WithStack(context.Canceled)) }()
169169

170170
caller, err := e.opt.SessionManager.Get(timeoutCtx, sessionID, false)
171171
if err != nil {

0 commit comments

Comments
 (0)