Skip to content

Commit 09648f4

Browse files
committed
replace WithTimeout with WithTimeoutCause
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 8a2a3e8 commit 09648f4

File tree

24 files changed

+88
-61
lines changed

24 files changed

+88
-61
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ linters-settings:
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)?$'
4444
- '^context\.WithCancel(# use context\.WithCancelCause instead)?$'
45+
- '^context\.WithTimeout(# use context\.WithTimeoutCause instead)?$'
46+
- '^context\.WithDeadline(# use context\.WithDeadline instead)?$'
4547
- '^ctx\.Err(# use context\.Cause instead)?$'
4648
importas:
4749
alias:

cache/remotecache/azblob/exporter.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ func (ce *exporter) uploadManifest(ctx context.Context, manifestKey string, read
150150
return errors.Wrap(err, "error creating container client")
151151
}
152152

153-
ctx, cnclFn := context.WithTimeout(ctx, time.Minute*5)
154-
defer cnclFn()
153+
ctx, cnclFn := context.WithCancelCause(ctx)
154+
ctx, _ = context.WithTimeoutCause(ctx, time.Minute*5, errors.WithStack(context.DeadlineExceeded))
155+
defer cnclFn(errors.WithStack(context.Canceled))
155156

156157
_, err = blobClient.Upload(ctx, reader, &azblob.BlockBlobUploadOptions{})
157158
if err != nil {
@@ -170,8 +171,9 @@ func (ce *exporter) uploadBlobIfNotExists(ctx context.Context, blobKey string, r
170171
return errors.Wrap(err, "error creating container client")
171172
}
172173

173-
uploadCtx, cnclFn := context.WithTimeout(ctx, time.Minute*5)
174-
defer cnclFn()
174+
uploadCtx, cnclFn := context.WithCancelCause(ctx)
175+
uploadCtx, _ = context.WithTimeoutCause(uploadCtx, time.Minute*5, errors.WithStack(context.DeadlineExceeded))
176+
defer cnclFn(errors.WithStack(context.Canceled))
175177

176178
// Only upload if the blob doesn't exist
177179
eTagAny := azblob.ETagAny

cache/remotecache/azblob/utils.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ func createContainerClient(ctx context.Context, config *Config) (*azblob.Contain
133133
}
134134
}
135135

136-
ctx, cnclFn := context.WithTimeout(ctx, time.Second*60)
137-
defer cnclFn()
136+
ctx, cnclFn := context.WithCancelCause(ctx)
137+
ctx, _ = context.WithTimeoutCause(ctx, time.Second*60, errors.WithStack(context.DeadlineExceeded))
138+
defer cnclFn(errors.WithStack(context.Canceled))
138139

139140
containerClient, err := serviceClient.NewContainerClient(config.Container)
140141
if err != nil {
@@ -148,8 +149,9 @@ func createContainerClient(ctx context.Context, config *Config) (*azblob.Contain
148149

149150
var se *azblob.StorageError
150151
if errors.As(err, &se) && se.ErrorCode == azblob.StorageErrorCodeContainerNotFound {
151-
ctx, cnclFn := context.WithTimeout(ctx, time.Minute*5)
152-
defer cnclFn()
152+
ctx, cnclFn := context.WithCancelCause(ctx)
153+
ctx, _ = context.WithTimeoutCause(ctx, time.Minute*5, errors.WithStack(context.DeadlineExceeded))
154+
defer cnclFn(errors.WithStack(context.Canceled))
153155
_, err := containerClient.Create(ctx, &azblob.ContainerCreateOptions{})
154156
if err != nil {
155157
return nil, errors.Wrapf(err, "failed to create cache container %s", config.Container)
@@ -177,8 +179,9 @@ func blobExists(ctx context.Context, containerClient *azblob.ContainerClient, bl
177179
return false, errors.Wrap(err, "error creating blob client")
178180
}
179181

180-
ctx, cnclFn := context.WithTimeout(ctx, time.Second*60)
181-
defer cnclFn()
182+
ctx, cnclFn := context.WithCancelCause(ctx)
183+
ctx, _ = context.WithTimeoutCause(ctx, time.Second*60, errors.WithStack(context.DeadlineExceeded))
184+
defer cnclFn(errors.WithStack(context.Canceled))
182185
_, err = blobClient.GetProperties(ctx, &azblob.BlobGetPropertiesOptions{})
183186
if err == nil {
184187
return true, nil

cache/remotecache/local/local.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ func getContentStore(ctx context.Context, sm *session.Manager, g session.Group,
105105
if sessionID == "" {
106106
return nil, errors.New("local cache exporter/importer requires session")
107107
}
108-
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
109-
defer cancel()
108+
timeoutCtx, cancel := context.WithCancelCause(context.Background())
109+
timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded))
110+
defer cancel(errors.WithStack(context.Canceled))
110111

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

client/build_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func testClientGatewayContainerPID1Tty(t *testing.T, sb integration.Sandbox) {
933933
output := bytes.NewBuffer(nil)
934934

935935
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
936-
ctx, timeout := context.WithTimeout(ctx, 10*time.Second)
936+
ctx, timeout := context.WithTimeoutCause(ctx, 10*time.Second, nil)
937937
defer timeout()
938938

939939
st := llb.Image("busybox:latest")
@@ -1015,7 +1015,7 @@ func testClientGatewayContainerCancelPID1Tty(t *testing.T, sb integration.Sandbo
10151015
output := bytes.NewBuffer(nil)
10161016

10171017
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
1018-
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
1018+
ctx, cancel := context.WithTimeoutCause(ctx, 10*time.Second, nil)
10191019
defer cancel()
10201020

10211021
st := llb.Image("busybox:latest")
@@ -1141,7 +1141,7 @@ func testClientGatewayContainerExecTty(t *testing.T, sb integration.Sandbox) {
11411141
inputR, inputW := io.Pipe()
11421142
output := bytes.NewBuffer(nil)
11431143
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
1144-
ctx, timeout := context.WithTimeout(ctx, 10*time.Second)
1144+
ctx, timeout := context.WithTimeoutCause(ctx, 10*time.Second, nil)
11451145
defer timeout()
11461146
st := llb.Image("busybox:latest")
11471147

@@ -1233,7 +1233,7 @@ func testClientGatewayContainerCancelExecTty(t *testing.T, sb integration.Sandbo
12331233
inputR, inputW := io.Pipe()
12341234
output := bytes.NewBuffer(nil)
12351235
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
1236-
ctx, timeout := context.WithTimeout(ctx, 10*time.Second)
1236+
ctx, timeout := context.WithTimeoutCause(ctx, 10*time.Second, nil)
12371237
defer timeout()
12381238
st := llb.Image("busybox:latest")
12391239

@@ -2132,7 +2132,7 @@ func testClientGatewayContainerSignal(t *testing.T, sb integration.Sandbox) {
21322132
product := "buildkit_test"
21332133

21342134
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
2135-
ctx, timeout := context.WithTimeout(ctx, 10*time.Second)
2135+
ctx, timeout := context.WithTimeoutCause(ctx, 10*time.Second, nil)
21362136
defer timeout()
21372137

21382138
st := llb.Image("busybox:latest")

client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9832,7 +9832,7 @@ func testLLBMountPerformance(t *testing.T, sb integration.Sandbox) {
98329832
def, err := st.Marshal(sb.Context())
98339833
require.NoError(t, err)
98349834

9835-
timeoutCtx, cancel := context.WithTimeout(sb.Context(), time.Minute)
9835+
timeoutCtx, cancel := context.WithTimeoutCause(sb.Context(), time.Minute, nil)
98369836
defer cancel()
98379837
_, err = c.Solve(timeoutCtx, def, SolveOpt{}, nil)
98389838
require.NoError(t, err)

cmd/buildctl/common/common.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ func ResolveClient(c *cli.Context) (*client.Client, error) {
9191

9292
timeout := time.Duration(c.GlobalInt("timeout"))
9393
if timeout > 0 {
94-
ctx2, cancel := context.WithTimeout(ctx, timeout*time.Second)
94+
ctx2, cancel := context.WithCancelCause(ctx)
95+
ctx2, _ = context.WithTimeoutCause(ctx2, timeout*time.Second, errors.WithStack(context.DeadlineExceeded))
9596
ctx = ctx2
96-
defer cancel()
97+
defer cancel(errors.WithStack(context.Canceled))
9798
}
9899

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

control/gateway/gateway.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ func (gwf *GatewayForwarder) lookupForwarder(ctx context.Context) (gateway.LLBBr
5959
return nil, errors.New("no buildid found in context")
6060
}
6161

62-
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
63-
defer cancel()
62+
ctx, cancel := context.WithCancelCause(ctx)
63+
ctx, _ = context.WithTimeoutCause(ctx, 3*time.Second, errors.WithStack(context.DeadlineExceeded))
64+
defer cancel(errors.WithStack(context.Canceled))
6465

6566
go func() {
6667
<-ctx.Done()

executor/containerdexecutor/executor.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,21 +371,22 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
371371
}
372372
}()
373373

374-
var cancel func()
374+
var cancel func(error)
375375
var killCtxDone <-chan struct{}
376376
ctxDone := ctx.Done()
377377
for {
378378
select {
379379
case <-ctxDone:
380380
ctxDone = nil
381381
var killCtx context.Context
382-
killCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
382+
killCtx, cancel = context.WithCancelCause(context.Background())
383+
killCtx, _ = context.WithTimeoutCause(killCtx, 10*time.Second, errors.WithStack(context.DeadlineExceeded))
383384
killCtxDone = killCtx.Done()
384385
p.Kill(killCtx, syscall.SIGKILL)
385386
io.Cancel()
386387
case status := <-statusCh:
387388
if cancel != nil {
388-
cancel()
389+
cancel(errors.WithStack(context.Canceled))
389390
}
390391
trace.SpanFromContext(ctx).AddEvent(
391392
"Container exited",
@@ -411,7 +412,7 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
411412
return nil
412413
case <-killCtxDone:
413414
if cancel != nil {
414-
cancel()
415+
cancel(errors.WithStack(context.Canceled))
415416
}
416417
io.Cancel()
417418
return errors.Errorf("failed to kill process on cancel")

executor/runcexecutor/executor.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,9 @@ func (k procKiller) Kill(ctx context.Context) (err error) {
532532

533533
// this timeout is generally a no-op, the Kill ctx should already have a
534534
// shorter timeout but here as a fail-safe for future refactoring.
535-
ctx, timeout := context.WithTimeout(ctx, 10*time.Second)
536-
defer timeout()
535+
ctx, cancel := context.WithCancelCause(ctx)
536+
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, errors.WithStack(context.DeadlineExceeded))
537+
defer cancel(errors.WithStack(context.Canceled))
537538

538539
if k.pidfile == "" {
539540
// for `runc run` process we use `runc kill` to terminate the process
@@ -615,17 +616,17 @@ func runcProcessHandle(ctx context.Context, killer procKiller) (*procHandle, con
615616
for {
616617
select {
617618
case <-ctx.Done():
618-
killCtx, timeout := context.WithTimeout(context.Background(), 7*time.Second)
619+
killCtx, timeout := context.WithCancelCause(context.Background())
620+
killCtx, _ = context.WithTimeoutCause(killCtx, 7*time.Second, errors.WithStack(context.DeadlineExceeded))
619621
if err := p.killer.Kill(killCtx); err != nil {
620622
select {
621623
case <-killCtx.Done():
622-
timeout()
623624
cancel(errors.WithStack(context.Cause(ctx)))
624625
return
625626
default:
626627
}
627628
}
628-
timeout()
629+
timeout(errors.WithStack(context.Canceled))
629630
select {
630631
case <-time.After(50 * time.Millisecond):
631632
case <-p.ended:
@@ -673,10 +674,11 @@ func (p *procHandle) WaitForReady(ctx context.Context) error {
673674
// We wait for up to 10s for the runc pid to be reported. If the started
674675
// callback is non-nil it will be called after receiving the pid.
675676
func (p *procHandle) WaitForStart(ctx context.Context, startedCh <-chan int, started func()) error {
676-
startedCtx, timeout := context.WithTimeout(ctx, 10*time.Second)
677-
defer timeout()
677+
ctx, cancel := context.WithCancelCause(ctx)
678+
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, errors.WithStack(context.DeadlineExceeded))
679+
defer cancel(errors.WithStack(context.Canceled))
678680
select {
679-
case <-startedCtx.Done():
681+
case <-ctx.Done():
680682
return errors.New("go-runc started message never received")
681683
case runcPid, ok := <-startedCh:
682684
if !ok {

0 commit comments

Comments
 (0)