From e366584728a12cc2ffb06012b4409e0a8b3d68a7 Mon Sep 17 00:00:00 2001 From: Jiaqiang Huang Date: Fri, 26 Jul 2024 17:17:19 +0800 Subject: [PATCH] ddl: fix a data race on localRowCntListener Written() (#54484) close pingcap/tidb#54373 --- pkg/ddl/backfilling.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/ddl/backfilling.go b/pkg/ddl/backfilling.go index 6e11fd7e5eb59..4fbed0422726d 100644 --- a/pkg/ddl/backfilling.go +++ b/pkg/ddl/backfilling.go @@ -782,12 +782,17 @@ type localRowCntListener struct { // prevPhysicalRowCnt records the row count from previous physical tables (partitions). prevPhysicalRowCnt int64 // curPhysicalRowCnt records the row count of current physical table. - curPhysicalRowCnt int64 + curPhysicalRowCnt struct { + cnt int64 + mu sync.Mutex + } } func (s *localRowCntListener) Written(rowCnt int) { - s.curPhysicalRowCnt += int64(rowCnt) - s.reorgCtx.setRowCount(s.prevPhysicalRowCnt + s.curPhysicalRowCnt) + s.curPhysicalRowCnt.mu.Lock() + s.curPhysicalRowCnt.cnt += int64(rowCnt) + s.reorgCtx.setRowCount(s.prevPhysicalRowCnt + s.curPhysicalRowCnt.cnt) + s.curPhysicalRowCnt.mu.Unlock() s.counter.Add(float64(rowCnt)) }