From 7fe2fd2c1776fd66971ab532edb99cf37a9d3461 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Thu, 22 Aug 2024 11:58:51 +0800 Subject: [PATCH] util: fix data race in sqlkiller --- pkg/executor/executor.go | 2 +- pkg/util/sqlkiller/sqlkiller.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index ed60ad95b2dfdd..33f625179e03b1 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -1856,7 +1856,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { vars.MemTracker.Killer = &vars.SQLKiller vars.DiskTracker.Killer = &vars.SQLKiller vars.SQLKiller.Reset() - vars.SQLKiller.ConnID = vars.ConnectionID + vars.SQLKiller.ConnID.Store(vars.ConnectionID) isAnalyze := false if execStmt, ok := s.(*ast.ExecuteStmt); ok { diff --git a/pkg/util/sqlkiller/sqlkiller.go b/pkg/util/sqlkiller/sqlkiller.go index 1abae2dd3cc247..27c972b4394588 100644 --- a/pkg/util/sqlkiller/sqlkiller.go +++ b/pkg/util/sqlkiller/sqlkiller.go @@ -42,7 +42,7 @@ const ( // SQLKiller is used to kill a query. type SQLKiller struct { Signal killSignal - ConnID uint64 + ConnID atomic.Uint64 // FinishFuncLock is used to ensure that Finish is not called and modified at the same time. // An external call to the Finish function only allows when the main goroutine to be in the writeResultSet process. // When the main goroutine exits the writeResultSet process, the Finish function will be cleared. @@ -58,7 +58,7 @@ func (killer *SQLKiller) SendKillSignal(reason killSignal) { if atomic.CompareAndSwapUint32(&killer.Signal, 0, reason) { status := atomic.LoadUint32(&killer.Signal) err := killer.getKillError(status) - logutil.BgLogger().Warn("kill initiated", zap.Uint64("connection ID", killer.ConnID), zap.String("reason", err.Error())) + logutil.BgLogger().Warn("kill initiated", zap.Uint64("connection ID", killer.ConnID.Load()), zap.String("reason", err.Error())) } } @@ -115,7 +115,7 @@ func (killer *SQLKiller) HandleSignal() error { failpoint.Inject("randomPanic", func(val failpoint.Value) { if p, ok := val.(int); ok { if rand.Float64() > (float64)(p)/1000 { - if killer.ConnID != 0 { + if killer.ConnID.Load() != 0 { targetStatus := rand.Int31n(5) atomic.StoreUint32(&killer.Signal, uint32(targetStatus)) } @@ -126,7 +126,7 @@ func (killer *SQLKiller) HandleSignal() error { err := killer.getKillError(status) if status == ServerMemoryExceeded { logutil.BgLogger().Warn("global memory controller, NeedKill signal is received successfully", - zap.Uint64("conn", killer.ConnID)) + zap.Uint64("conn", killer.ConnID.Load())) } return err } @@ -134,7 +134,7 @@ func (killer *SQLKiller) HandleSignal() error { // Reset resets the SqlKiller. func (killer *SQLKiller) Reset() { if atomic.LoadUint32(&killer.Signal) != 0 { - logutil.BgLogger().Warn("kill finished", zap.Uint64("conn", killer.ConnID)) + logutil.BgLogger().Warn("kill finished", zap.Uint64("conn", killer.ConnID.Load())) } atomic.StoreUint32(&killer.Signal, 0) }