diff --git a/pkg/executor/batch_point_get.go b/pkg/executor/batch_point_get.go index 4392605e40a24..0c999879fac5d 100644 --- a/pkg/executor/batch_point_get.go +++ b/pkg/executor/batch_point_get.go @@ -238,7 +238,8 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error { batchGetter := e.batchGetter if e.Ctx().GetSessionVars().MaxExecutionTime > 0 { // If MaxExecutionTime is set, we need to set the context deadline for the batch get. - ctx, _ = context.WithTimeout(ctx, time.Duration(e.Ctx().GetSessionVars().MaxExecutionTime)*time.Millisecond) + ctx, cancel = context.WithTimeout(ctx, time.Duration(e.Ctx().GetSessionVars().MaxExecutionTime)*time.Millisecond) + defer cancel() } rc := e.Ctx().GetSessionVars().IsPessimisticReadConsistency() if e.idxInfo != nil && !isCommonHandleRead(e.tblInfo, e.idxInfo) { diff --git a/pkg/executor/point_get.go b/pkg/executor/point_get.go index 51aa939c335b0..2839070dcbcff 100644 --- a/pkg/executor/point_get.go +++ b/pkg/executor/point_get.go @@ -675,7 +675,8 @@ func (e *PointGetExecutor) get(ctx context.Context, key kv.Key) ([]byte, error) // if not read lock or table was unlock then snapshot get if e.Ctx().GetSessionVars().MaxExecutionTime > 0 { // if the query has max execution time set, we need to set the context deadline for the get request - ctxWithTimeout, _ := context.WithTimeout(ctx, time.Duration(e.Ctx().GetSessionVars().MaxExecutionTime)*time.Millisecond) + ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Duration(e.Ctx().GetSessionVars().MaxExecutionTime)*time.Millisecond) + defer cancel() return e.snapshot.Get(ctxWithTimeout, key) } else { return e.snapshot.Get(ctx, key) diff --git a/pkg/store/copr/batch_coprocessor.go b/pkg/store/copr/batch_coprocessor.go index 57c36883b218f..b28d5a812c494 100644 --- a/pkg/store/copr/batch_coprocessor.go +++ b/pkg/store/copr/batch_coprocessor.go @@ -1117,7 +1117,8 @@ func (c *CopClient) sendBatch(ctx context.Context, req *kv.Request, vars *tikv.V if req.MaxExecutionTime > 0 { // If MaxExecutionTime is set, we need to set the deadline for the whole batch coprocessor request context. - ctxWithTimeout, _ := context.WithTimeout(bo.GetCtx(), time.Duration(req.MaxExecutionTime)*time.Millisecond) + ctxWithTimeout, cancel := context.WithTimeout(bo.GetCtx(), time.Duration(req.MaxExecutionTime)*time.Millisecond) + defer cancel() bo.TiKVBackoffer().SetCtx(ctxWithTimeout) } diff --git a/pkg/store/copr/coprocessor.go b/pkg/store/copr/coprocessor.go index 41de1f8b26efd..b99499f1b4cf3 100644 --- a/pkg/store/copr/coprocessor.go +++ b/pkg/store/copr/coprocessor.go @@ -351,7 +351,8 @@ func buildCopTasks(bo *Backoffer, ranges *KeyRanges, opt *buildCopTaskOpt) ([]*c if req.MaxExecutionTime > 0 { // If the request has a MaxExecutionTime, we need to set the deadline of the context. - ctxWithTimeout, _ := context.WithTimeout(bo.GetCtx(), time.Duration(req.MaxExecutionTime)*time.Millisecond) + ctxWithTimeout, cancel := context.WithTimeout(bo.GetCtx(), time.Duration(req.MaxExecutionTime)*time.Millisecond) + defer cancel() bo.TiKVBackoffer().SetCtx(ctxWithTimeout) }