diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index 9fdb82cba3..cac879acd4 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -299,8 +299,8 @@ func TestShow(t *testing.T) { tk.MustQuery("SHOW PROCEDURE STATUS WHERE Db='test'").Check(testkit.Rows()) tk.MustQuery("SHOW TRIGGERS WHERE `Trigger` ='test'").Check(testkit.Rows()) - tk.MustQuery("SHOW PROCESSLIST;").Check(testkit.Rows("1 test Sleep 0 autocommit SHOW PROCESSLIST;")) - tk.MustQuery("SHOW FULL PROCESSLIST;").Check(testkit.Rows("1 test Sleep 0 autocommit SHOW FULL PROCESSLIST;")) + tk.MustQuery("SHOW PROCESSLIST;").Check(testkit.Rows(fmt.Sprintf("%d test Sleep 0 autocommit SHOW PROCESSLIST;", tk.Session().ShowProcess().ID))) + tk.MustQuery("SHOW FULL PROCESSLIST;").Check(testkit.Rows(fmt.Sprintf("%d test Sleep 0 autocommit SHOW FULL PROCESSLIST;", tk.Session().ShowProcess().ID))) tk.MustQuery("SHOW EVENTS WHERE Db = 'test'").Check(testkit.Rows()) tk.MustQuery("SHOW PLUGINS").Check(testkit.Rows()) tk.MustQuery("SHOW PROFILES").Check(testkit.Rows()) diff --git a/session/session.go b/session/session.go index fca5505540..bbd819d28b 100644 --- a/session/session.go +++ b/session/session.go @@ -3550,15 +3550,21 @@ func (s *session) setRequestSource(ctx context.Context, stmtLabel string, stmtNo // RemoveLockDDLJobs removes the DDL jobs which doesn't get the metadata lock from job2ver. func RemoveLockDDLJobs(s Session, job2ver map[int64]int64, job2ids map[int64]string) { - if s.GetSessionVars().InRestrictedSQL { + sv := s.GetSessionVars() + if sv.InRestrictedSQL { return } - s.GetSessionVars().GetRelatedTableForMDL().Range(func(tblID, value any) bool { + sv.TxnCtxMu.Lock() + defer sv.TxnCtxMu.Unlock() + if sv.TxnCtx == nil { + return + } + sv.GetRelatedTableForMDL().Range(func(tblID, value any) bool { for jobID, ver := range job2ver { ids := util.Str2Int64Map(job2ids[jobID]) if _, ok := ids[tblID.(int64)]; ok && value.(int64) < ver { delete(job2ver, jobID) - logutil.BgLogger().Debug("old running transaction block DDL", zap.Int64("table ID", tblID.(int64)), zap.Uint64("conn ID", s.GetSessionVars().ConnectionID)) + logutil.BgLogger().Debug("old running transaction block DDL", zap.Int64("table ID", tblID.(int64)), zap.Uint64("conn ID", sv.ConnectionID)) } } return true diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index a3feb75033..db3606baf6 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -642,6 +642,8 @@ type SessionVars struct { RetryInfo *RetryInfo // TxnCtx Should be reset on transaction finished. TxnCtx *TransactionContext + // TxnCtxMu is used to protect TxnCtx. + TxnCtxMu sync.Mutex // TxnManager is used to manage txn context in session TxnManager interface{} diff --git a/sessiontxn/isolation/base.go b/sessiontxn/isolation/base.go index e7e6891be1..2db2797ea9 100644 --- a/sessiontxn/isolation/base.go +++ b/sessiontxn/isolation/base.go @@ -119,7 +119,9 @@ func (p *baseTxnContextProvider) OnInitialize(ctx context.Context, tp sessiontxn if p.onInitializeTxnCtx != nil { p.onInitializeTxnCtx(txnCtx) } + sessVars.TxnCtxMu.Lock() sessVars.TxnCtx = txnCtx + sessVars.TxnCtxMu.Unlock() if variable.EnableMDL.Load() { sessVars.TxnCtx.EnableMDL = true } diff --git a/sessiontxn/staleread/provider.go b/sessiontxn/staleread/provider.go index 84904ab18f..9bbc4c5593 100644 --- a/sessiontxn/staleread/provider.go +++ b/sessiontxn/staleread/provider.go @@ -119,6 +119,7 @@ func (p *StalenessTxnContextProvider) activateStaleTxn() error { if err != nil { return errors.Trace(err) } + sessVars.TxnCtxMu.Lock() sessVars.TxnCtx = &variable.TransactionContext{ TxnCtxNoNeedToRestore: variable.TxnCtxNoNeedToRestore{ InfoSchema: is, @@ -129,6 +130,7 @@ func (p *StalenessTxnContextProvider) activateStaleTxn() error { TxnScope: txnScope, }, } + sessVars.TxnCtxMu.Unlock() if interceptor := temptable.SessionSnapshotInterceptor(p.sctx, is); interceptor != nil { txn.SetOption(kv.SnapInterceptor, interceptor)