diff --git a/domain/domain.go b/domain/domain.go index f0fd3966d6536..af1e46a0f0cbf 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -1322,7 +1322,8 @@ func (do *Domain) StartLoadStatsSubWorkers(ctxList []sessionctx.Context) { statsHandle := do.StatsHandle() for i, ctx := range ctxList { statsHandle.StatsLoad.SubCtxs[i] = ctx - do.wg.Run(func() { statsHandle.SubLoadWorker(ctx, do.exit) }) + do.wg.Add(1) + go statsHandle.SubLoadWorker(ctx, do.exit, &do.wg) } } diff --git a/statistics/handle/handle_hist.go b/statistics/handle/handle_hist.go index 3c4f6f4aa8154..53ae365f8ce8b 100644 --- a/statistics/handle/handle_hist.go +++ b/statistics/handle/handle_hist.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/statistics" + "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/sqlexec" "go.uber.org/zap" @@ -136,9 +137,10 @@ type StatsReaderContext struct { } // SubLoadWorker loads hist data for each column -func (h *Handle) SubLoadWorker(ctx sessionctx.Context, exit chan struct{}) { +func (h *Handle) SubLoadWorker(ctx sessionctx.Context, exit chan struct{}, exitWg *util.WaitGroupWrapper) { readerCtx := &StatsReaderContext{} defer func() { + exitWg.Done() logutil.BgLogger().Info("SubLoadWorker exited.") if readerCtx.reader != nil { err := h.releaseStatsReader(readerCtx.reader, ctx.(sqlexec.RestrictedSQLExecutor))