Skip to content

Commit 7eb2c8e

Browse files
authored
Merge pull request #4457 from tonistiigi/cancel-cause
replace context.WithCancel with WithCancelCause
2 parents 4ab32be + 09648f4 commit 7eb2c8e

File tree

59 files changed

+274
-226
lines changed

Some content is hidden

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

59 files changed

+274
-226
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ 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+
- '^context\.WithTimeout(# use context\.WithTimeoutCause instead)?$'
46+
- '^context\.WithDeadline(# use context\.WithDeadline instead)?$'
47+
- '^ctx\.Err(# use context\.Cause instead)?$'
4448
importas:
4549
alias:
4650
- 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
}

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: 8 additions & 8 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

@@ -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)
@@ -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.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: 3 additions & 3 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)
@@ -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)

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

0 commit comments

Comments
 (0)