Skip to content

[Bug]: recursive CTE without anchor overflows planner stack and exits service #32600

Done

Description

@Ariznawlll

Description

An invalid recursive CTE whose two UNION members both read the CTE, with no nonrecursive anchor, causes the entire MatrixOne service process to exit with fatal error: stack overflow. MySQL 8.0 rejects the statement normally with error 3574. A SQL validation error must never take down the service.

Environment

  • Branch: main
  • Commit: c8accd138bb91144050ff173100afd7a71a02888 (fixed Nov 12 daily main)
  • Deployment: task-owned local isolated cluster, one LogService, one TN, two CN listeners at 16001/16002 in one mo-service process; MySQL 8.0.45 local socket as oracle.

Steps to reproduce

No table or data is needed:

SELECT 1;

WITH RECURSIVE qn AS (
  SELECT 1 FROM qn
  UNION ALL
  SELECT 1 FROM qn
)
SELECT * FROM qn;

Run the invalid query from either CN listener. Then reconnect and run SELECT 1 again.

Actual behavior

  • Client receives ERROR 2013: Lost connection to MySQL server during query.
  • Both local CN listeners refuse new connections because the mo-service process exited.
  • Service log contains runtime: goroutine stack exceeds 1000000000-byte limit followed by fatal error: stack overflow.
  • The repeated stack segment is QueryBuilder.buildTable → bindRecursiveCte → bindCte → buildTable, including pkg/sql/plan/query_builder.go:13852, :14335, :14390, :6037, and :6305.

Expected behavior

Reject the SQL with a normal syntax/recursive-CTE validation error, keep the service available, and leave state unchanged. MySQL 8.0.45 returns:

ERROR 3574: Recursive Common Table Expression 'qn' should have one or more non-recursive query blocks followed by one or more recursive ones

Stability and controls

  • Reproducer: 3/3 service exits. First from the adapted official MySQL mysql-test/include/with_recursive.inc:31 case on CN1; then minimal stand-alone SQL on CN2 and CN1 after independent service restarts.
  • Before each minimal run, MO SELECT 1 succeeded; MySQL returned the expected error and stayed available. A bounded legal recursive CTE returned (1),(2),(3) in both databases in three independent fresh schemas. After each MO crash, reconnect failed; after restarting the owned service, both CN listeners returned git_version() = c8accd138b and SELECT 1 normally.
  • The failing SQL is read-only, requires no fixture, and cannot alter data. No data atomicity claim beyond this read-only case is made.

Evidence

  • Official source case: mysql-test/include/with_recursive.inc:31 at MySQL source SHA 9fc8d60003b4eee32767c1e30942f222c3508c31 (8.0.46 test source). The adapted replay recorded both MySQL error 3574 and MO connection loss at event 17.
  • Local artifacts: /private/tmp/mo-group32455-H2kxsk/mysql-cte-replay-20261112-132952/events.jsonl, /private/tmp/mo-group32455-H2kxsk/cte-stack-probe-16002-1794461625949621000.json, /private/tmp/mo-group32455-H2kxsk/cte-stack-probe-16001-1794461690065478000.json.
  • Local owned service log: /private/tmp/mo-group32455-H2kxsk/sdk-main-20261112-run/server.log, three fatal error: stack overflow occurrences. Relevant excerpt:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable
    pkg/sql/plan/query_builder.go:14390
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindRecursiveCte
    pkg/sql/plan/query_builder.go:6037
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindCte
    pkg/sql/plan/query_builder.go:6305

Code analysis

The stack proves unbounded planner recursion while binding a CTE with no anchor; it is not an executor error or expected SQL rejection. In pkg/sql/plan/query_builder.go:13041, splitRecursiveMemberWithMode removes the self-referencing right UNION member, but the remaining left member also references qn. There is no check that the final left member is nonrecursive before bindRecursiveCte binds it as the initial/anchor statement at :6023-6037. That binding resolves qn through buildTable/bindCte and repeats indefinitely. This is the confirmed planner path for this shape; the appropriate validation/error placement still needs a fix design.

Regression coverage

Add a SQL BVT/MOTR case based on the minimal statement, asserting a bounded SQL error and SELECT 1 on both CN listeners afterward; add a planner unit test for two self-referencing UNION members with no anchor. Preserve a legal bounded recursive CTE control.

Related

Activity

  1. added this to the MO-202611 milestone on Nov 12, 2026
  2. added
    kind/bug
    on Nov 12, 2026
  3. added theissue type on Nov 12, 2026
  4. XuPeng-SH commented on Nov 29, 2026

    @XuPeng-SH
    Contributor

    Consolidated duplicate #32601 here before its requested deletion.

    Additional independent evidence retained from #32601:

    • Reproduced on main e2459cc55f4b514893e19732743460bc5f16d552 (2026-11-09), macOS arm64 with 16 GiB RAM, using the single-node -launch etc/launch.toml deployment. The same no-anchor UNION ALL statement terminated the combined mo-service process in two standalone runs and once during official-suite replay.
    • In those runs the client waited about 15 seconds before ERROR 2013; later connections were refused. No panic or stack trace was emitted to fd. That missing log does not establish a different root cause: the SQL and failure boundary are the same as above, while this issue retains the independently captured stack-overflow trace.
    • Controls: adding a nonrecursive anchor (SELECT 1 AS a UNION ALL SELECT 1 FROM qn) succeeds; the no-UNION pure self-reference (WITH RECURSIVE qn AS (SELECT 1 FROM qn) ...) is rejected without crashing. This further isolates the failure to the UNION shape's missing anchor validation.
  5. Ariznawlll commented on Nov 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    QA 验收通过(最新 main)

    版本与方法

    • 被测及开测时官方 main:383c17fb4bc6e150bd5b5fe8a2408ce298dc4c6c;关闭前再次解析仍为该 SHA。
    • 独立双 CN,真实 MySQL 协议;两个 listener 分别以 SELECT git_version() 核对到上述完整 SHA。
    • 连续 3 轮独立数据库实测,未把旧 main 预检计入通过结果。

    逐轮覆盖与结果:3/3 PASS

    1. 在两个 CN 分别执行原始无 anchor 自引用 UNION ALL,以及 UNION DISTINCT、两个无 anchor CTE、嵌套 derived table、INSERT ... WITH RECURSIVE 共 5 种形态;均正常返回 1064,消息包含 initial statement of Recursive Common Table Expression,没有连接丢失或服务退出。
    2. 每次拒绝后同连接健康查询成功,基表始终只含 (7);合法递归 CTE 精确返回 (1),(2),(3)。
    3. 错误 PREPARE 同样返回上述 1064;合法 prepared recursive CTE 反复绑定上界 1/3/5,精确返回对应行集。
    4. CN2 新连接重复全部拒绝、原表和健康控制;服务日志无 fatal error、stack overflow 或 goroutine stack exceeds,最终 runner 正常退出。

    三轮日志 SHA256:

    • round 1:ae04eab871a4c087fc703d37cc5e61361bc92ead2d014cc64a489303aaaa7054
    • round 2:319e0b88ac612e2716ec9571dcd1e7af379e462fb89cbe3171af50553ebdc220
    • round 3:7bdbd96408e170d6cf9af2d4716b58dcac9655a1e8cbff8956751c6ef692f00c

    MOTR 回归与 CI 状态

    原始 canonical Issue #32600 的回归已存在于 issue_32601_recursive_anchor.py,并已进入 matrixorigin/motr PR 374,当前 head 65059e95c10936aeb601a81a99a974ef351d2377;无需新增重复用例。截至评论时,hashbuild-boundary 为 FAILED,linux、HA、两个 branch-acceptance、main-native-sql、main-native-engine 等仍为 pending(queued/in_progress),daily-42 为 SKIPPED。本单关闭依据是上述当前 SHA 的真实服务验收,不把其他 workflow 或 pending 状态当作通过证据。

    结论:无 anchor recursive CTE 导致整个服务退出的原始问题已在最新 main 按双 CN 原始复现、扩展形态、合法对照和拒绝后健康检查通过验收,关闭为 COMPLETED。

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions