Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(*Handle).HandleUpdateStats can trigger data race #33108

Closed
xuyifangreeneyes opened this issue Mar 15, 2022 · 0 comments · Fixed by #33260
Closed

(*Handle).HandleUpdateStats can trigger data race #33108

xuyifangreeneyes opened this issue Mar 15, 2022 · 0 comments · Fixed by #33260
Labels
affects-6.0 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@xuyifangreeneyes
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Just run TiDB server. Maybe turning on stats feedback feature make it easier to reproduce.

// HandleUpdateStats update the stats using feedback.
func (h *Handle) HandleUpdateStats(is infoschema.InfoSchema) error {
ctx := context.Background()
tables, _, err := h.execRestrictedSQL(ctx, "SELECT distinct table_id from mysql.stats_feedback")
if err != nil {
return errors.Trace(err)
}
if len(tables) == 0 {
return nil
}
for _, ptbl := range tables {
// this func lets `defer` works normally, where `Close()` should be called before any return
err = func() error {
tbl := ptbl.GetInt64(0)
const sql = "select table_id, hist_id, is_index, feedback from mysql.stats_feedback where table_id=%? order by hist_id, is_index"
rc, err := h.mu.ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), sql, tbl)
if err != nil {
return errors.Trace(err)
}

At line 744 we use h.mu.ctx to execute internal sql without h.mu.Lock(). h.mu.Lock() is needed because we only allow h.mu.ctx executing one statement at a time. Without h.mu.Lock(), data race may occur when h.mu.ctx executes two statements concurrently.

2. What did you expect to see? (Required)

No data race.

3. What did you see instead (Required)

Data race happened.

fatal error: concurrent map writes

goroutine 1185 [running]:
runtime.throw(0x3d8e07c, 0x15)
/usr/local/go/src/runtime/panic.go:1117 +0x72 fp=0xc01df45428 sp=0xc01df453f8 pc=0x12b9dd2
runtime.mapassign_fast64(0x38e00c0, 0xc00ec840c0, 0x7, 0x6186720)
/usr/local/go/src/runtime/map_fast64.go:176 +0x325 fp=0xc01df45468 sp=0xc01df45428 pc=0x1294765
github.com/pingcap/tidb/util/execdetails.(*RuntimeStatsColl).RegisterStats(0xc0012b64c8, 0x7, 0x42ba8a8, 0xc077907480)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/execdetails/execdetails.go:591 +0x4be fp=0xc01df45500 sp=0xc01df45468 pc=0x1fe755e
github.com/pingcap/tidb/executor.(*ProjectionExec).Close(0xc01bc47900, 0x0, 0x0)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/projection.go:327 +0x246 fp=0xc01df45568 sp=0xc01df45500 pc=0x331a8c6
github.com/pingcap/tidb/executor.(*SortExec).Close(0xc036d9efc0, 0x3128ea48d406, 0x338b9a5)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/sort.go:81 +0xee fp=0xc01df455a8 sp=0xc01df45568 pc=0x336cf0e
github.com/pingcap/tidb/executor.(*recordSet).Close(0xc03572d220, 0x136, 0xc0933a7718)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/adapter.go:171 +0x38 fp=0xc01df455e8 sp=0xc01df455a8 pc=0x31cceb8
github.com/pingcap/tidb/session.(*execStmtResult).Close(0xc0817faf00, 0x0, 0x68)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/session/session.go:1733 +0x42 fp=0xc01df45648 sp=0xc01df455e8 pc=0x33e57c2
github.com/pingcap/tidb/util/sqlexec.RecordSet.Close-fm(0xc00dfa3900, 0xc036d9efc0)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/sqlexec/restricted_sql_executor.go:147 +0x38 fp=0xc01df45670 sp=0xc01df45648 pc=0x2126658
github.com/pingcap/tidb/parser/terror.Call(0xc01df45910)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/parser/terror/terror.go:298 +0x3f fp=0xc01df457f0 sp=0xc01df45670 pc=0x1c32c9f
github.com/pingcap/tidb/statistics/handle.(*Handle).HandleUpdateStats.func1(0xc0933a79e0, 0xc00e3a0e10, 0x42f9098, 0xc013990d90, 0x0, 0x0)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/statistics/handle/update.go:732 +0x787 fp=0xc01df45948 sp=0xc01df457f0 pc=0x2652b07
github.com/pingcap/tidb/statistics/handle.(*Handle).HandleUpdateStats(0xc00e3a0e10, 0x42f9098, 0xc013990d90, 0x0, 0x6)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/statistics/handle/update.go:733 +0x126 fp=0xc01df45a00 sp=0xc01df45948 pc=0x26448e6
github.com/pingcap/tidb/domain.(*Domain).updateStatsWorker(0xc00032fe60, 0x4303f18, 0xc00d5fde00, 0x42d70c8, 0xc00e3a0ea0)
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/domain/domain.go:1394 +0x925 fp=0xc01df45fb8 sp=0xc01df45a00 pc=0x28391a5
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc01df45fc0 sp=0xc01df45fb8 pc=0x12f5081
created by github.com/pingcap/tidb/domain.(*Domain).UpdateTableStatsLoop
/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/domain/domain.go:1259 +0x1d6

4. What is your TiDB version? (Required)

v5.3.1(I think the problem also exists on master)

@xuyifangreeneyes xuyifangreeneyes added the type/bug The issue is confirmed as a bug. label Mar 15, 2022
@ti-chi-bot ti-chi-bot added may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. labels Mar 17, 2022
@hawkingrei hawkingrei removed may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. labels Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-6.0 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants