From 357da30e67b189d2ad85567a9cd9e87b8659254b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=B6=85?= Date: Tue, 13 Dec 2022 15:16:52 +0800 Subject: [PATCH] *: fix slow query nil pointer panic when get session for TTL (#39865) close pingcap/tidb#39864 --- executor/adapter.go | 2 +- ttl/ttlworker/session.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/executor/adapter.go b/executor/adapter.go index 5f01aa51e0378..5e12cce1ccc69 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -1538,7 +1538,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) { if a.retryCount > 0 { slowItems.ExecRetryTime = costTime - sessVars.DurationParse - sessVars.DurationCompile - time.Since(a.retryStartTime) } - if _, ok := a.StmtNode.(*ast.CommitStmt); ok { + if _, ok := a.StmtNode.(*ast.CommitStmt); ok && sessVars.PrevStmt != nil { slowItems.PrevStmt = sessVars.PrevStmt.String() } slowLog := sessVars.SlowLogFormat(slowItems) diff --git a/ttl/ttlworker/session.go b/ttl/ttlworker/session.go index 1514c1746f348..6326fff5f11d7 100644 --- a/ttl/ttlworker/session.go +++ b/ttl/ttlworker/session.go @@ -60,7 +60,8 @@ func getSession(pool sessionPool) (session.Session, error) { pool.Put(resource) }) - if _, err = se.ExecuteSQL(context.Background(), "commit"); err != nil { + // Force rollback the session to guarantee the session is not in any explicit transaction + if _, err = se.ExecuteSQL(context.Background(), "ROLLBACK"); err != nil { se.Close() return nil, err }