From 86a6694de0733dc771440ec97996159036468136 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 30 Jan 2023 19:59:55 +0800 Subject: [PATCH] bindInfo: fix datarace on bindings (#40846) close pingcap/tidb#40843 --- bindinfo/bind_record.go | 11 +++++++++++ executor/BUILD.bazel | 1 - executor/show.go | 6 ++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bindinfo/bind_record.go b/bindinfo/bind_record.go index 6395bbaa278ba..50e8e0ba20784 100644 --- a/bindinfo/bind_record.go +++ b/bindinfo/bind_record.go @@ -115,6 +115,17 @@ type BindRecord struct { Bindings []Binding } +// Copy get the copy of bindRecord +func (br *BindRecord) Copy() *BindRecord { + nbr := &BindRecord{ + OriginalSQL: br.OriginalSQL, + Db: br.Db, + } + nbr.Bindings = make([]Binding, len(br.Bindings)) + copy(nbr.Bindings, br.Bindings) + return nbr +} + // HasEnabledBinding checks if there are any enabled bindings in bind record. func (br *BindRecord) HasEnabledBinding() bool { for _, binding := range br.Bindings { diff --git a/executor/BUILD.bazel b/executor/BUILD.bazel index 8b0e824288c99..c8881bec70917 100644 --- a/executor/BUILD.bazel +++ b/executor/BUILD.bazel @@ -192,7 +192,6 @@ go_library( "//util/servermemorylimit", "//util/set", "//util/size", - "//util/slice", "//util/sqlexec", "//util/stmtsummary", "//util/stringutil", diff --git a/executor/show.go b/executor/show.go index 36a9b8485822b..130f743d914bb 100644 --- a/executor/show.go +++ b/executor/show.go @@ -69,7 +69,6 @@ import ( "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/sem" "github.com/pingcap/tidb/util/set" - "github.com/pingcap/tidb/util/slice" "github.com/pingcap/tidb/util/sqlexec" "github.com/pingcap/tidb/util/stringutil" "golang.org/x/exp/slices" @@ -317,7 +316,10 @@ func (e *ShowExec) fetchShowBind() error { } else { tmp = domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord() } - bindRecords := slice.Copy(tmp) + bindRecords := make([]*bindinfo.BindRecord, 0) + for _, bindRecord := range tmp { + bindRecords = append(bindRecords, bindRecord.Copy()) + } // Remove the invalid bindRecord. ind := 0 for _, bindData := range bindRecords {