Skip to content

Commit

Permalink
sql: fix recent leak of a context
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
yuzefovich committed Jul 21, 2022
1 parent 457d724 commit d7e454a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ func (ex *connExecutor) execStmtInOpenState(
ast := parserStmt.AST
ctx = withStatement(ctx, ast)

var cancelQuery context.CancelFunc
ctx, cancelQuery = contextutil.WithCancel(ctx)

makeErrEvent := func(err error) (fsm.Event, fsm.EventPayload, error) {
ev, payload := ex.makeErrEvent(err, ast)
return ev, payload, nil
Expand Down Expand Up @@ -327,10 +324,9 @@ func (ex *connExecutor) execStmtInOpenState(
ex.planner.EvalContext().Placeholders = pinfo
}

var cancelQuery context.CancelFunc
ctx, cancelQuery = contextutil.WithCancel(ctx)
ex.addActiveQuery(ast, formatWithPlaceholders(ast, ex.planner.EvalContext()), queryID, cancelQuery)
if ex.executorType != executorTypeInternal {
ex.metrics.EngineMetrics.SQLActiveStatements.Inc(1)
}

// Make sure that we always unregister the query. It also deals with
// overwriting res.Error to a more user-friendly message in case of query
Expand All @@ -344,6 +340,7 @@ func (ex *connExecutor) execStmtInOpenState(
}
}
ex.removeActiveQuery(queryID, ast)
cancelQuery()
if ex.executorType != executorTypeInternal {
ex.metrics.EngineMetrics.SQLActiveStatements.Dec(1)
}
Expand Down Expand Up @@ -386,6 +383,10 @@ func (ex *connExecutor) execStmtInOpenState(
}
}(ctx, res)

if ex.executorType != executorTypeInternal {
ex.metrics.EngineMetrics.SQLActiveStatements.Inc(1)
}

p := &ex.planner
stmtTS := ex.server.cfg.Clock.PhysicalTime()
ex.statsCollector.Reset(ex.applicationStats, ex.phaseTimes)
Expand Down Expand Up @@ -505,7 +506,7 @@ func (ex *connExecutor) execStmtInOpenState(
timeoutTicker = time.AfterFunc(
timerDuration,
func() {
ex.cancelQuery(queryID)
cancelQuery()
queryTimedOut = true
doneAfterFunc <- struct{}{}
})
Expand Down

0 comments on commit d7e454a

Please sign in to comment.