Repository navigation
Description
Description
A SQL EXECUTE of an already prepared statement fails with an internal nil-pointer error when it is the second statement in one MySQL CLIENT_MULTI_STATEMENTS packet. The same prepared statement succeeds alone and when it is the first statement in the packet.
This is independent of multi-assignment SET: the minimal prepared statement is only SET @a = ?.
Environment
- Branch:
main - Commit:
d0057b3503611022cdf9a6e24c98065d209eb8e4 - Deployment: local isolated two-CN MatrixOne cluster, tested through the MySQL protocol on CN1 and CN2.
- Client:
github.com/go-sql-driver/mysqlwithmultiStatements=trueandinterpolateParams=false. - Date: 2026-08-07
Steps to reproduce
db, _ := sql.Open("mysql",
"root:111@tcp(127.0.0.1:6001)/?multiStatements=true&interpolateParams=false")
conn, _ := db.Conn(context.Background())
conn.ExecContext(ctx, "SET @a=99, @b='before'")
conn.ExecContext(ctx, "PREPARE p FROM 'SET @a=?'")
// One COM_QUERY packet containing two statements.
_, err := conn.ExecContext(ctx, "SET @x=41; EXECUTE p USING @x")
// Actual: ERROR 20101 ... panic runtime error: invalid memory address or nil pointer dereferenceActual behavior
EXECUTE returns ERROR 20101 (HY000): internal error: panic runtime error: invalid memory address or nil pointer dereference.
The stack starts at:
github.com/matrixorigin/matrixone/pkg/vm/process.(*Process).GetTxnOperator
pkg/frontend.buildPlanWithPrepareMode (mysql_cmd_executor.go:3057)
The tested failure leaves @a,@b as 99,before; after the error, the same physical connection can execute SELECT 1 successfully. Thus this reproduction did not observe partial session mutation, but a supported prepared-execution path fails with an internal error.
Expected behavior
The second statement should execute normally and leave @a = 41, just as it does when EXECUTE p USING @x is sent in its own packet.
Stability and controls
- Reproducer: 3/3, on CN1 -> CN2 -> CN1.
- Normal multi-statement control:
SET @control_a=1; SET @control_b=@control_a+1succeeds 3/3 and returns1,2. - Position control:
EXECUTE p USING @x; SET @tail=2succeeds 3/3 and returns@a=41,@tail=2. - Standalone prepared
SETand the text/binary multi-assignment cases fixed by fix(frontend): preserve params across prepared multi-SET #26705 succeed in a separate 3-round live test. - Failure atomicity: after each failing packet,
@a,@bremains99,before. - Connection reuse: after each failure,
SELECT 1returns1on the same connection.
Code analysis
The observed stack reaches buildPlanWithPrepareMode line 3057, which calls ctx.GetProcess().GetTxnOperator() before a nil check of the returned transaction operator. The first statement in a multi-statement packet appears to leave the compiler context for the later prepared EXECUTE without a process. This is a code-level hypothesis; the protocol-level reproduction above is independent of it.
Regression coverage
Add a protocol regression that enables CLIENT_MULTI_STATEMENTS, prepares SET @a=?, and verifies both statement orders:
SET @x=41; EXECUTE p USING @xmust set@a=41.EXECUTE p USING @x; SET @tail=2must remain valid.
Include same-connection reuse and no-partial-mutation assertions after any execution failure.
Related
- [Bug]: Prepared multi-assignment SET panics and partially updates session variables #26685 / fix(frontend): preserve params across prepared multi-SET #26705 fixed a different prepared multi-assignment
SETfailure. This issue uses one assignment and only reproduces when itsEXECUTEis a later statement in a multi-statement packet.
Activity
fix(frontend): guard prepared multi-statement planning without a proc…
slyunnn commentedon Aug 19, 2026
Verification Environment
MatrixOne origin/main: 621782d — fix variables (#26762).
Topology: 1 Log + 1 TN + 2 CN + 1 Proxy.
Endpoints: direct CN1 127.0.0.1:16001, direct CN2 127.0.0.1:16002, and Proxy 127.0.0.1:6009 (local equivalent of
Docker-exposed 6001).
Client protocol: Go github.com/go-sql-driver/mysql, one physical database/sql.Conn, MySQL COM_QUERY with
multiStatements=true and interpolateParams=false.
Related fix: 4d81724 / PR #27227 (#27227), fix(frontend): guard
prepared multi-statement planning without a process (#27227).
What Was Covered
Failed — original scenario SET @x=41; EXECUTE issue_26766_stmt USING @x still returned ERROR 20101 with a nil-parent
panic on CN1, CN2, and Proxy.
Failed — replay of the original scenario reproduced the same failure on all three endpoints.
OK — ordinary CLIENT_MULTI_STATEMENTS ordering: SET @control_a=1; SET @control_b=@control_a+1.
OK — compatibility ordering: EXECUTE ...; SET @tail=2 succeeds when EXECUTE is the first statement in the packet.
OK — standalone prepared EXECUTE succeeds.
OK — invalid standalone EXECUTE returns a normal error, preserves prior session state, and the connection remains
reusable with SELECT 1.
Not covered — a passing later invalid EXECUTE scenario, because the original later prepared EXECUTE path still
panics first.
Not covered — large-data, block/batch, spill, concurrency, network-failure, and data-skew scenarios.
Findings
4d81724 guards ctx.GetProcess() in buildPlanWithPrepareMode, but the same function subsequently passes
ctx.GetContext() to AttachBuildPlanMarkKey. In the reproduced later-statement path, that context is nil, causing
context.WithValue to panic.
The failure is deterministic across all three endpoints and is independent of table rows or data volume.
Final Conclusion
Not fixed. The current origin/main still panics for a prepared EXECUTE used as a later entry in one CLIENT_MULTI_STATEMENTS packet.
daviszhen commentedon Aug 22, 2026
A second fix is available in PR #27471.
The follow-up reproducer showed that the previous process guard still left the session compiler context pointing at a closed temporary
ExecCtxafter the synthetic SELECT used to evaluateSET @x=41; the next preparedEXECUTEthen passed a nil context tocontext.WithValue. The new patch restores the caller context after SET evaluation and adds a nil-context fallback in plan tracing, with frontend and race regressions covering the path.Please re-run the original multi-statement verification against the new PR head.
fix(frontend): restore compiler context for prepared multi-statements (…
slyunnn commentedon Aug 24, 2026
Regression and extended verification for #26766 (2026-08-24)
Verification Environment
Validated against MatrixOne
origin/maincommit861a0f5ea6cf90cd278cf6d4cdb1c74234cf36039—feat: esql_tvf / sql_tvf table functions and ENGINE = ESQL|SQL foreign external tables (#27489).Topology:
1Log service +1TN service +2CN services +1Proxy service. The three test endpoints were Proxy127.0.0.1:26009, CN1127.0.0.1:26001, and CN2127.0.0.1:26002.The client used Go
database/sqlwithgithub.com/go-sql-driver/mysql, a single physical*sql.Conn, MySQLCLIENT_MULTI_STATEMENTSviamultiStatements=true, andinterpolateParams=false.Related fixes: #27227, commit
4d817248,fix(frontend): guard prepared multi-statement planning without a process; and the completing fix #27471, commit48e4601fe5042f270ee404dcf16263fde69ab246,fix(frontend): restore compiler context for prepared multi-statements.What Was Covered
COM_QUERYpacket containingSET @x=41; EXECUTE issue_26766_stmt USING @xcompleted without a panic;@a=41and@b='before'were verified.EXECUTEin a multi-statement packet succeeded on Proxy, CN1, and CN2.SET @a=99; EXECUTE issue_26766_stmt USING @x; SET @tail=2preserved@a=41and@tail=2.CLIENT_MULTI_STATEMENTSexecutionSET @control_a=1; SET @control_b=@control_a+1preserved statement order.SET @a=77; EXECUTE issue_26766_missing USING @xreturned a normal SQL error, did not expose a panic, preserved@a=77, and left the same connection usable.EXECUTEand a subsequentSELECT 1succeeded on the same physical connection.Findings
The failure was fixed by restoring the caller compiler context after synthetic planning for a preceding
SETstatement and by guarding the missing-process path. The original later-EXECUTEpacket no longer produces the prior nil-context panic.Large-data regression is not needed. The defect depends on statement ordering and compiler-context lifecycle within one protocol packet, not row count, block/batch boundaries, memory/spill, concurrency, skew, network transfer volume, or resource scale.
MOTR / BVT Coverage
Added and recorded/replayed the protocol regression on MOTR branch
test/issue-26766-prepared-multi-statement:script/14_issue_regression/issue_26766_prepared_multi_statement_protocol.goscript/14_issue_regression/issue_26766_prepared_multi_statement_protocol.shscript/14_issue_regression/out/issue_26766_prepared_multi_statement_protocol.outsuites/14_issue_regression/t/issue_26766_prepared_multi_statement.testsuites/14_issue_regression/r/issue_26766_prepared_multi_statement.resultMOTR protocol record and replay both passed against Proxy
127.0.0.1:26009. The same real-protocol replay also passed against direct CN1127.0.0.1:26001and CN2127.0.0.1:26002.Commit:
ffeb0560d9fc9fb70420b9f4377c38cb7dc028a1(test: add MOTR regression for #26766).Final Conclusion
Fixed. The original MySQL
CLIENT_MULTI_STATEMENTSreproducer, extended ordering and error/reuse controls, and three-endpoint topology verification all passed on the latestorigin/main. A large-data regression is not needed because data scale is unrelated to this compiler-context and protocol-statement-order defect.fix(frontend): guard prepared multi-statement planning without a proc…
fix(frontend): restore compiler context for prepared multi-statements (…
fix(frontend): guard prepared multi-statement planning without a proc…
fix(frontend): restore compiler context for prepared multi-statements (…
fix(frontend): guard prepared multi-statement planning without a proc…
fix(frontend): restore compiler context for prepared multi-statements (…