Skip to content

mo_table_stats betaTask goroutine stuck in select for 46.5 hours, causing transaction leak #25400

Closed as not planned

Description

@aptend

Summary

In the freetier-01 production environment, a betaTask goroutine in mo_table_stats.go was stuck in a select statement for 46.5 hours, holding an open transaction that could never be cleaned up.

Environment

  • Cluster: freetier-01 (prod)
  • Pod: freetier-01-s2-56c954c888-cn-qxjh7
  • MO version: production build deployed ~3 days ago

Symptoms

  1. Leak checker reported every 20 minutes:
    ERROR cn-service found leak txn
      txn-id: 0acdcbd9d5f822ce18bd902a36285465
      create-at: 2026/06/30 15:03:02  (46+ hours ago)
      session: migrate-goRoutineId 0
      lockWaitTimeout: 31536000000000000 (~1 year)
    

Root Cause

Stuck goroutine (from ETL leakcheck profile)

goroutine 997023 [select, 2793 minutes]:   <-- 46.5 hours!
github.com/matrixorigin/matrixone/pkg/vm/engine/disttae.(*dynamicCtx).betaTask(...)
    mo_table_stats.go:2029                  <-- stuck in select
github.com/matrixorigin/matrixone/pkg/vm/engine/disttae.initMoTableStatsConfig.func1.8.1()
    mo_table_stats.go:464

Code path (mo_table_stats.go)

betaTask is a long-lived goroutine that processes table statistics across multiple alphaTask rounds:

func (d *dynamicCtx) betaTask(ctx context.Context, service string, eng engine.Engine) {
    for {
        select {
        case <-ctx.Done():        // exit on context cancellation
            return
        case tbl := <-d.tblQueue: // receive next table to process
            // process stats...
        }
        // *** NO idle timeout, NO heartbeat ***
    }
}

The select only has two cases:

  1. ctx.Done() — but turn2SysCtx() only wraps with a tenant value, does NOT create a cancellable context
  2. d.tblQueue — fed by alphaTask, which is called by tableStatsExecutor periodically

Why it gets stuck permanently

After alphaTask finishes feeding tables and tableStatsExecutor resets its ticker (executeTicker.Reset(d.conf.UpdateDuration)), betaTask enters the select and waits for the next round. If the executor is blocked (e.g. prepare() hangs on a SQL query) or the stats task is disabled (checkMoveOnTask() returns true), betaTask will wait indefinitely with no self-recovery mechanism.

Transaction leak chain

alphaTask → subscribeTable() → creates txn (0acdcbd9...)
  → PartitionState sent to betaTask via d.tblQueue
    → betaTask processes, then enters select waiting for more
      → 46.5 hours pass with no new data
        → goroutine still alive → txn never cleaned up
          → lockWaitTimeout=1 year → not killed by timeout

Suggested Fix

  1. Add idle timeout to betaTask select:
select {
case <-ctx.Done():
    return
case tbl := <-d.tblQueue:
    // process...
case <-time.After(maxIdleDuration):  // NEW: idle timeout
    return  // alphaTask will relaunch on next round
}
  1. Use cancellable context for betaTask: wrap with context.WithCancel so when tableStatsExecutor exits, betaTask is properly cancelled.

  2. Reduce migrate session lockWaitTimeout: 1 year is unreasonable; use a sensible default.

Attached

  • qxjh7_leakcheck_goroutine_20260702.txt.gz — Full goroutine dump from the ETL leakcheck profile (750KB decompressed). Search for goroutine 997023 to find the stuck goroutine.

Activity

  1. aptend commented on Jul 2, 2026

    @aptend
    ContributorAuthor

    ETL leakcheck goroutine dump attached:

    qxjh7_leakcheck_goroutine_20260702.txt.gz

    • 750KB decompressed text goroutine dump
    • Search goroutine 997023 to find the stuck betaTask (46.5h in select)
    • Search mo_table_stats for all related goroutines
  2. added
    kind/bug
    on Jul 2, 2026
  3. added theissue type on Jul 2, 2026
  4. added this to the MO-202607 milestone on Jul 2, 2026
  5. added
    severity/s0
    on Jul 6, 2026
  6. aptend commented on Jul 6, 2026

    @aptend
    ContributorAuthor

    Update after re-checking the dump/log correlation:

    The current evidence is not enough to prove that the leaked txn is stuck in mo_table_stats.betaTask.

    What we can confirm from the attached goroutine dump:

    goroutine 997023 [select, 2793 minutes]:
    github.com/matrixorigin/matrixone/pkg/vm/engine/disttae.(*dynamicCtx).betaTask(...)
    

    This is a long-lived table stats background goroutine. The betaTask code is expected to stay in a select waiting for either ctx.Done() or d.tblQueue, so a long [select] duration alone does not prove it is stuck or related to the leaked txn.

    The leaked txn log contains:

    ConnectionID: 4165805386
    SessionID: 019f190e-197c-7879-ac76-941c731491d4
    txn-id: 0acdcbd9d5f822ce18bd902a36285465
    goRoutineId: 827864991936
    

    However, these values do not appear in the attached goroutine dump, and there is no reliable join from this txn/session/connection to goroutine 997023.

    One important finding: the goRoutineId printed in frontend session_info comes from github.com/petermattis/goid, while goroutine dump ids come from the runtime stack header (goroutine N [...]). The goid package reads Go runtime internal g.goid layout directly, and this can become unreliable when the Go runtime layout changes. In local verification, go test github.com/petermattis/goid fails with a mismatched id. That means the logged goRoutineId cannot currently be trusted as a stable key to match against goroutine dump ids.

    Suggested follow-up:

    1. Do not treat this dump as proof that mo_table_stats.betaTask caused the txn leak.
    2. Consider changing the frontend goroutine id logging path to use the same mechanism as goroutine dumps, e.g. parse the current goroutine id from runtime.Stack(buf, false), at low-frequency points such as NewRoutine / migration begin and keep the cached value.
    3. After that change, future leak logs can be correlated with goroutine dumps using the same goroutine id space.

    This may add a small cost if done via runtime.Stack, so it should stay cached and should not be recomputed for every log line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions