Skip to content

Commit

Permalink
Add defer cancel to avoid memory leakage
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoW30 committed Nov 20, 2024
1 parent beaa704 commit e117b01
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pkg/executor/batch_point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/store/copr/batch_coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/store/copr/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit e117b01

Please sign in to comment.