Skip to content

[Bug]: Single recursive CTE with pure self-referencing seed kills the whole server process #32601

Done

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues. Searches for "recursive cte crash", "server crash", "process killed recursive" returned nothing matching this plan-time failure.

Branch Name

  • main (worktree at HEAD of main)

Commit ID

  • e2459cc55f4b514893e19732743460bc5f16d552 (2026-11-09 build, mo-service)

Other Environment Information

  • Hardware parameters: Apple M-series (arm64), 16 GiB RAM
  • OS type: macOS (darwin arm64)
  • Single-node launch config (-launch etc/launch.toml, one CN + one TN + logservice via local fileservice)

Current Behavior

A single SQL statement kills the entire server process (CN + TN + logservice in one mo-service process — nothing survives, the port stops listening, and a full restart is required):

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

Observed sequence on the client:

  1. Query hangs for ~15 s, then the connection dies with (2013, 'Lost connection to MySQL server during query')
  2. The mo-service process is gone (ps shows nothing; port 26001 stops listening)
  3. Every subsequent connection attempt fails with (2003, Connection refused) until manually restarted

Reproduced twice with 100% determinism, plus once more during an official-suite replay (this exact statement is item #7 of the official mysql-test/include/with_recursive.inc, which is how it was first discovered — the remaining 150 statements of that file run fine).

Expected Behavior

MySQL 8.3 semantics: this statement is invalid SQL usage (the recursive union branch references the CTE in a non-recursive position), and MySQL rejects it at plan time with an error such as:

ERROR 1146 (42S02): Table 'test.qn' doesn't exist

The correct MO behavior is any plan/parse-time rejection — never a process kill. A single client-supplied query expression should never be able to take down the whole database node (availability DoS).

Steps to Reproduce

# pip install pymysql
import pymysql, time

conn = pymysql.connect(host='127.0.0.1', port=26001, user='root', password='111',
                       autocommit=True, connect_timeout=5, read_timeout=25)
cur = conn.cursor()
cur.execute("""
    WITH RECURSIVE qn AS (SELECT 1 FROM qn UNION ALL SELECT 1 FROM qn)
    SELECT * FROM qn
""")   # <-- server fully dies here

Then verify the process is dead:

lsof -i :26001 -P -n | grep LISTEN   # nothing
ps aux | grep mo-service               # nothing

Minimal characterization of the crater:

  • WITH RECURSIVE qn AS (SELECT 1 AS a UNION ALL SELECT 1 FROM qn) SELECT * FROM qn — works fine (a proper seed branch exists). The crash requires the seed branch itself to reference qn (SELECT ... FROM qn in the non-recursive member).
  • WITH RECURSIVE qn AS (SELECT 1 FROM qn) SELECT * FROM qn (no UNION at all) — rejected at parse time with "In recursive query block ...", no crash.
  • So the trigger is: plan-time passage of a non-terminating pure self-reference that only unfolds inside the union-all branch shape — the check that guards the non-union form does not cover the union shape.

Additional Statements / Logs

No panic message or stack trace was emitted to fd before death (the process vanished without logging a panic — itself worth investigating: kill -9-style exit, no graceful shutdown log lines either). Startup logs before the crash show normal operation; after restart with the same data directory the cluster comes back cleanly (no data corruption observed).

Activity

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

    @XuPeng-SH
    Contributor

    Fixed on main by #32831 (27bb33d97b), which also addresses the duplicate report #32795. The planner now rejects an invalid recursive CTE anchor before it can re-enter binding. The merged PR reports SQL error 1064 and successful reuse of the same connection after the error.

    On main at 6540533196, seven focused planner checks passed under the race detector. MySQL TCP validation also passed for finite recursion, prepared statement reuse with different parameters, and a subsequent query on the same connection.

    @Ariznawlll, please verify this on a main build that includes #32831 and confirm that the connection remains usable after the planner returns the validation error.

  5. Ariznawlll commented on Nov 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。本轮“递归 CTE 缺少合法 anchor 导致服务退出”已完成下列原始复现、相关扩展及对应 Linux CI;回归已提交至现有 MOTR PR,按授权关闭。

    关键证据

    版本与修复范围

    • 测试基线:2026-11-30 00:00 时段固定的官方 main 6540533196711f09e6b1279066063cb90043ebd8,独立目录编译、独立端口/数据目录;两个 CN 的 SELECT git_version() 与完整 SHA 一致。
    • 关联修复 PR:fix(plan): reject recursive CTE anchor self-references #32831;main merge:27bb33d97b35c581b11775cb1ffbcbd41de35139。
    • 本轮未修改 MatrixOne 产品实现,未向 MatrixOne 主库提交测试 PR。

    实际执行步骤与结果

    1. 执行纯自引用种子及其 UNION 变体、两个 CTE 互相引用、嵌套 derived-table 自引用和 INSERT ... WITH RECURSIVE 变体;均以预期 1064 拒绝。
    2. 每次拒绝之后立即执行原表查询及有限递归 CTE,正常返回原行和 1、2、3;服务没有退出,连接仍可继续使用。
    3. 错误递归 PREPARE 被拒绝;合法 prepared 递归语句反复绑定上界 1/3/5,精确结果正确;另一 CN 的新连接也正常。
    4. 本地 MOTR 三轮和审计轮均通过。已有 TestRecursiveCteAnchorReentry 三轮通过,包含独立子进程与受控栈边界,避免只凭 SQL 错误判断服务安全。

    回归与 CI

    • 已追加现有 MOTR #374,用例:script/14_issue_regression/issue_32601_recursive_anchor.py。
    • Linux CI 46284168418 已完成 SUCCESS,下载 artifact 核对实际 MO SHA 为 e75a4d2ca470b35c2bf254cfcf5e3069f51be90a,不是仅依据分支名称。六组正式 MOTR × 3 轮,共 18/18(0 SKIP);plan、blockio、fulltext、vector metric 定向源码测试,以及 frontend native prepared SHOW 和 sqlintegration 角色规则测试均 -count=3 通过。
    • 每日调度须等相应 PR 合入才生效,本次是已执行的 PR CI,不声称已在默认分支每日运行。
    • 次数按脚本完整轮次统计,不把 MOTR 报告的一个 script statement 当成内部 SQL 条数。

    待解决点

    本 Issue 所列验收范围内无待解决的产品失败。MOTR PR 尚待合入;已获得“回归提交后可先关闭”的授权。本结论不外推到未涉及的性能、大数据或其他 Issue。

    最终结论:测试通过。上述原始与扩展场景、对应分支回归及 Linux CI 均通过;未通过项:无。回归已提交现有 MOTR PR,本次关闭 #32601。

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

Metadata

Metadata

Assignees

Labels

area/frontendkind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions