Repository navigation
Description
[Bug Report] WITH RECURSIVE CTE referencing the recursive table twice causes server crash (fatal error: stack overflow)
Summary
A single WITH RECURSIVE statement that references the recursive table twice in the recursive member causes the whole mo-service process to crash with fatal error: stack overflow (exit code 2), instead of returning a semantic error like MySQL does.
The recursive descent happens in the plan builder: buildTable → bindCte → bindRecursiveCte → bindSelect → bindSelectClause → buildFrom → buildTable → ... loops forever until the goroutine stack exceeds the 1 GB limit.
This is a server-crashing (DoS-class) bug: any client able to run a recursive CTE can take down the whole MatrixOne instance.
Environment
| Item | Value |
|---|---|
| MatrixOne version | 8.0.30-MatrixOne-v1.3.0 (development build) |
| Git commit | 094c0b33bd9f0a37a76f242367c4ad6a3fa39ac8 |
| Branch | codex/32670-tenant-index-read (commit subject: "fix: allow tenant admin read-only index inspection", 2026-11-13) |
| Source repo | https://github.com/matrixorigin/matrixone |
| OS / Arch | macOS 14.5, darwin/arm64 (Apple M3, 16 GB) |
| Go toolchain | go1.27.1-X:simd darwin/arm64 |
| Client | mysql CLI + PyMySQL (MySQL protocol, 127.0.0.1:6001) |
| Deployment | single-node local launch: ./mo-service -launch ./etc/launch/launch.toml |
Minimal Reproduction
Step 1 — start a standalone MatrixOne instance (single-node):
./mo-service -launch ./etc/launch/launch.tomlStep 2 — run this single statement. No database creation, no USE, no session variables needed — a bare query is enough:
mysql -h 127.0.0.1 -P 6001 -u dump -p111 -e \
"WITH RECURSIVE qn AS (SELECT 1 FROM qn UNION ALL SELECT 1 FROM qn) SELECT * FROM qn;"or as plain SQL:
WITH RECURSIVE qn AS (
SELECT 1 FROM qn
UNION ALL
SELECT 1 FROM qn
)
SELECT * FROM qn;Observed: the client immediately gets
ERROR 2013 (HY000): Lost connection to MySQL server during query
and the mo-service process terminates shortly after with exit code 2. The instance must be restarted manually; it does not recover on its own.
Verified with the stock mysql CLI too (not just the programmatic client), so the crash is client-independent:
$ mysql -h 127.0.0.1 -P 6001 -u dump -p111 -e "
CREATE DATABASE IF NOT EXISTS d1; USE d1;
WITH RECURSIVE qn AS (SELECT 1 FROM qn UNION ALL SELECT 1 FROM qn) SELECT * FROM qn;"
ERROR 2013 (HY000) at line 3: Lost connection to MySQL server during query
# mo-service then exits with code 2 (fatal error: stack overflow)Expected Behavior (MySQL 8.0 reference)
MySQL rejects this statement with a semantic error (the recursive table may only be referenced once, and not in a subquery), keeping the server alive:
ERROR 3575 (HY000): In recursive query block of Recursive Common Table Expression 'qn',
the recursive table must be referenced only once, and not in any subquery.
MO already handles several other malformed recursive CTEs correctly (e.g. WITH RECURSIVE qn AS (SELECT 1 FROM qn) SELECT * FROM qn returns error 1064 with the proper message). The double reference case is the one that slips through validation and crashes.
Actual Behavior — Crash Log
runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0x... stack=[0x..., 0x...]
fatal error: stack overflow
runtime stack:
runtime.throw(...)
runtime.newstack()
runtime.morestack()
goroutine 4592 [running]:
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:13772
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:14403
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:14348
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildFrom(...) query_builder.go:13035
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindSelectClause(...) query_builder.go:9490
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindSelect(...) query_builder.go:6741
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindRecursiveCte(...) query_builder.go:6050
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).bindCte(...) query_builder.go:6318
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:13865
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:14403
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).buildTable(...) query_builder.go:14348
... (the same buildTable → bindCte → bindRecursiveCte → bindSelect → bindSelectClause → buildFrom → buildTable cycle repeats thousands of times until stack exhaustion)
Key cycle (package pkg/sql/plan, file query_builder.go):
buildTable (13865)
→ bindCte (6318)
→ bindRecursiveCte (6050)
→ bindSelect (6741)
→ bindSelectClause (9490)
→ buildFrom (13035)
→ buildTable (14348 / 14403) // re-enters the same CTE
→ bindCte (6318) // ... and loops again
Root-Cause Analysis
bindRecursiveCte (query_builder.go:6050) re-enters plan building for the recursive member. When the recursive table qn is referenced twice, the second reference re-triggers bindCte → bindRecursiveCte for the same, still-being-bound CTE. There is no "binding in progress / already-bound" guard, so the builder recurses on itself indefinitely instead of either:
- returning the "recursive table must be referenced only once" semantic error (the correct behavior), or
- recognizing the self-reference and stopping the recursion.
The fix should add a re-entrancy guard (mark the CTE as being bound and error out / short-circuit on a second entry) and/or enforce the single-reference rule before descending into bindRecursiveCte.
Additional Context
- Discovered while running the upstream MySQL functional test suite (
mysql-test/t/with_recursive.test, which--sourcesinclude/with_recursive.inc) against MatrixOne for compatibility comparison. The crashing statement is one of the early cases in that file. - The same crash reproduces both when the statement is run alone (minimal repro above) and as part of the full test sequence. Because the infinite recursion happens in a goroutine, the process sometimes dies a few seconds after the statement returns, which can make the trigger look non-deterministic — but the stack trace is always the same
bindRecursiveCtecycle. - Other malformed recursive CTEs in the same file (self-reference without anchor, recursion before anchor, mutual recursion
qn ↔ qn2) are rejected correctly; only the double-reference form crashes the server.
Severity
Critical / High — unauthenticated-by-design SQL input that reliably crashes the entire database process (availability impact, DoS).
Activity
Ariznawlll commentedon Nov 16, 2026
Additional finding — this is a whole bug class, not a single statement.
I fuzzed the recursive-CTE space around the original repro. The crash is triggered by any form in which the recursive table is referenced more than once inside the recursive member, regardless of syntax:
SELECT 1 FROM qn UNION ALL SELECT 1 FROM qn(2 refs)UNION ALL×2)UNION(dedup) instead ofUNION ALLINSERT INTO t WITH RECURSIVE ...UNIONFROM qn JOIN qn q2 ON ...)1064 unsupport multiple recursive table expr in recursive CTE(no crash)WHEREsubqueryUseful clue for the fix: the self-join case is already caught by an existing guard that returns
unsupport multiple recursive table expr in recursive CTE. So MO does have a "multiple recursive table references" check — but it only covers some shapes (e.g. JOIN) and misses theUNION [ALL]double-reference path, which then falls into the unguardedbindRecursiveCterecursion and overflows the stack. Extending that existing single-reference check to the UNION/UNION ALL recursive-member path (before descending intobindRecursiveCte) should close the whole class.fix(plan): reject recursive CTE anchor self-references (#32831)
XuPeng-SH commentedon Nov 17, 2026
QA 交接(不是验收通过)
#32831 已于 2026-11-17 05:14 UTC 合入 main,merge SHA
27bb33d97b35c581b11775cb1ffbcbd41de35139。修复按 CTE declaration identity 拒绝 anchor 绑定期间对同一声明的重入,避免无限重复绑定;不是简单禁止所有多递归成员。合法递归、同名内层 CTE/别名、带 schema 的真实表和原有递归成员诊断保持各自语义。已核对完整 issue/PR 讨论、最终 approve 和五文件补丁,未见当前适用的已验证反例或未完成开发 follow-up。保持 open 交 QA,不作验收或自动 backport。证据与限制:本轮仅远端核对,未运行本地测试。作者在
f491aaab加修复上报告 17/17 focused subtests、完整 planner 包(5045 顶层、17949 direct subtests)通过;真实服务将原语句拒绝为 1064,同连接后续查询与有限递归正常,公共 BVT 的 nometa/严格 metadata 各 3/3。这些是历史 source-bound 结果,不是最终 merge SHA 新实测。随后 rebase 到e765c5fd时五文件内容及 diff 不变,作者报告 111 包 incremental SCA 通过,以及另一个 prepared-time 目标 normal/race 各 19 RUN/PASS;该目标不替代 CTE 回归。最终评审复用冻结 head 的 UT/SCA/coverage/双 multi-CN BVT 成功状态,无新本地运行或原始逐测试 CI 日志;merge queue 最终记录 CI Required 成功。此前 DROP DATABASE/heartbeat timeout 没有被独立重现或宣称彻底修复,不当作本 CTE 修复的新反例。建议 QA 在此 SHA 或后继 main 上复测原语句及三臂 UNION ALL、UNION DISTINCT、多 CTE、derived/outer UNION、INSERT SELECT 等 anchor 自引用变体,确认返回错误且服务不崩溃;同时覆盖同连接与 compiler context 拒绝后复用、合法单/多递归成员、词法遮蔽、schema-qualified 表,以及 self-join/子查询/列数不匹配的既有错误。完整修复后的生产环境可用性、跨版本行为仍需验证;不将历史诊断文字中的“多次引用”笼统扩展为禁止合法递归。
Ariznawlll commentedon Nov 17, 2026
4.2-dev (e8fd84b, 2026-11-17 tip) is still affected by this crash.
Verification setup:
mo-servicefrom a fresh shallow clone of4.2-devate8fd84b331f168eafd0ec28149b043eaebc7e39d(branch 4.2-dev, self-built with go1.26.5, macOS arm64).Minimal repro (same as the original report):
Result on 4.2-dev:
ERROR 2013 (Lost connection to MySQL server during query).mo-servicedies withfatal error: stack overflow(exit 2).So on 4.2-dev the anchor self-reference goes through the
seedNumericTableProjectionTypespath added for grouped ENUM/SET numeric handling, which recurses intobuildFrom -> buildTable -> bindCte -> bindSelect -> ...until the goroutine stack is exhausted.The fix in #32831 (reject recursive CTE anchor self-references, merged to main as 27bb33d) does not appear to have been backported to 4.2-dev: I don't see a backport PR, and the repro above still kills the process on the current 4.2-dev tip.
Suggested follow-up: backport the anchor self-reference rejection from #32831 to 4.2-dev, or add the equivalent re-entrancy guard on the declaration identity in the 4.2 codebase (note the recursion path there includes seedNumericTableProjectionTypes, so the guard should cover bindCte re-entry during that seeding path too).
Ariznawlll commentedon Nov 17, 2026
Verification on main tip: fix confirmed effective (follow-up to the QA handoff plan).
Verified on a self-built
mo-servicefrom main at919c9fc32f(>= fix merge27bb33d97b35c581b11775cb1ffbcbd41de35139, go1.27.1, macOS arm64, single-node launch, isolated ports, fresh bootstrap; listener binary/cwd/git_version triple-checked).Crash variants from the original report and the QA handoff matrix — all now rejected cleanly with
1064 SQL parser error: In the initial statement of Recursive Common Table Expression <name>, the recursive table must not ..., server stays up, and the same connection remains usable afterwards:WITH RECURSIVE qn AS (SELECT 1 FROM qn UNION ALL SELECT 1 FROM qn) SELECT * FROM qnWHERE 1 IN (SELECT 1 FROM qn))INSERT INTO sink WITH RECURSIVE qn AS (...) SELECT * FROM qnNegative controls (must keep working) — all pass:
WITH RECURSIVE seq(n) AS (...)→ 10 rows, correctcte_fix_verify.real_t) → recursion produces correct maxunsupport multiple recursive table expr)No hangs, no crashes, zero process restarts during the whole run. The rejection message and same-connection recoverability match what the QA handoff asked to verify.
(Cross-reference: the same repro still kills 4.2-dev at
e8fd84b331via theseedNumericTableProjectionTypespath — see my previous comment; that remains a backport gap, not a fix failure.)Ariznawlll commentedon Nov 17, 2026
Verified fixed on main tip — closing.
Fix #32831 (merged as 27bb33d) verified effective on a self-built main@919c9fc32f:
ERROR 1064rejecting the anchor reference, with the server staying up and the same connection remaining usable.issue_32795_recursive_cte_anchor_selfref) has been added to matrixorigin/motr (target-branch: main) covering all 12 scenarios with a recorded baseline.One open follow-up kept visible (not a fix defect): the fix has not been backported to 4.2-dev — the repro still kills 4.2-dev@e8fd84b3 via the
seedNumericTableProjectionTypespath (see earlier comment with the full stack). Backport tracking continues in the comments above and can be referenced by the release team.Ariznawlll commentedon Nov 17, 2026
Fix #32831 verified effective on main tip (919c9fc32f): all crash variants rejected with a clean 1064 error, legal recursion unaffected, regression case added to motr (target-branch: main). Closing as fixed on main; 4.2-dev backport gap documented in comments.
Ariznawlll commentedon Nov 17, 2026
Regression case landed in motr via matrixorigin/motr#355 (target-branch: main) — closes the loop on this issue's verification.
Ariznawlll commentedon Nov 17, 2026
重新打开:本 issue 存在“先关闭、后补 motr PR”的顺序问题。对应的 motr #355 于 2026-11-17 10:09 UTC 创建,晚于本 issue 的关闭时间,不符合“关闭前必须完成回归 PR”的要求。现恢复 Open。当前 #355 已包含本 issue 的用例,后续需核对该 PR 的完整验证与交付状态后,再统一给出关闭结论。
Ariznawlll commentedon Nov 17, 2026
QA 验收结论:main 通过;4.2-dev 原始崩溃仍可复现,保持 OPEN 转回研发
1. 实际验证版本
7fc45a2be2424d887199c5949362855cbacfe30f,SQL 确认git_version()为该完整 SHA,version() = 8.0.30-MatrixOne-v1.3.0。祖先链包含 planner 修复 #32831(27bb33d97b35c581b11775cb1ffbcbd41de35139)和后续多成员递归执行修复 #32828(43addc16ba30c3e5a0feb11447078092a5fc3953)。7400cae392e90fa94b8cf54dc8520c3c13647884,SQL 确认git_version()为该完整 SHA,version() = 8.0.30-MatrixOne-v4.2.9。该分支不包含 fix(plan): reject recursive CTE anchor self-references #32831 的修复 commit。2. main 实测:通过
UNION ALL、UNION DISTINCT、多 CTE、derived、outer UNION、INSERT SELECT等变体均返回 1064 错误,不再触发 stack overflow;同一连接可继续执行 SQL,测试后服务仍存活。PREPARE/EXECUTE/DEALLOCATE均符合预期。被拒绝的INSERT SELECT目标表行数为 0,合法 prepared 递归返回sum(n)=6。e1160015bb5895c1677d5fc1496c4bee440592b3(含 fix(plan): reject recursive CTE anchor self-references #32831、不含 [Bug] Fix multi-member recursive CTE frontier handling #32828)上,原始 crash 语句已被拒绝,但以下合法双递归成员超过 120 秒未完成,服务未崩溃:sum(n)=53。因此这是后续递归 frontier 执行问题的版本边界,不能误报为原始 stack overflow 未修复。3. 4.2-dev 实测:失败
在最新 4.2-dev 上重新执行原始语句:
结果:客户端返回
ERROR 2013 (Lost connection to MySQL server during query);随后 SQL 无法继续连接,mo-service以 exit 2 退出。日志明确包含:递归路径仍包含
seedNumericTableProjectionTypes、bindCte和bindRecursiveCte。这不是脚本错误或旧实例误判,而是 4.2-dev 上可重复的产品反例。请研发将 #32831 的 declaration-identity 重入防护适配/backport 到 4.2-dev,并覆盖 seeding 路径;在该分支通过前不应按全分支验收关闭。4. 回归与白盒证据
ec6a3daeff3dca872f4480e5137e463c99d309fb的 4 项 CI 全部 SUCCESS。14_issue_regression.issue_32795_recursive_cte_anchor_selfref(@target-branch: main)。-test.list核对注册,再运行测试二进制,未把[no tests to run]计为通过):TestRecursiveCteAnchorReentrySubprocess:PASS;TestRecursiveCteAnchorReentry:17/17 子测试 PASS;TestDispatchRecursiveCTEMemberForwardsEmptyRoundMarker、TestDispatchIntermediateForwardsRecursiveRoundMarker、TestDispatchRecursiveSinkStopsOnlyAfterEmptyGlobalFrontier:3/3 PASS。未验证:多 CN、race、完整 binary prepared 协议、长稳以及 4.2-dev 修复后的全矩阵。当前保持 OPEN 的唯一阻塞是上述 4.2-dev 原始崩溃;main 的 planner 修复及后续合法递归执行修复已有实测证据。