Skip to content

[Bug]: Prepared EXECUTE as a later multi-statement packet entry panics #26766

Closed

Description

@Ariznawlll

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/mysql with multiStatements=true and interpolateParams=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 dereference

Actual 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+1 succeeds 3/3 and returns 1,2.
  • Position control: EXECUTE p USING @x; SET @tail=2 succeeds 3/3 and returns @a=41,@tail=2.
  • Standalone prepared SET and 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,@b remains 99,before.
  • Connection reuse: after each failure, SELECT 1 returns 1 on 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:

  1. SET @x=41; EXECUTE p USING @x must set @a=41.
  2. EXECUTE p USING @x; SET @tail=2 must remain valid.

Include same-connection reuse and no-partial-mutation assertions after any execution failure.

Related

Activity

  1. added
    kind/bug
    on Aug 7, 2026
  2. self-assigned this
    on Aug 7, 2026
  3. slyunnn commented on Aug 19, 2026

    @slyunnn

    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.

  4. daviszhen commented on Aug 22, 2026

    @daviszhen
    Contributor

    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 ExecCtx after the synthetic SELECT used to evaluate SET @x=41; the next prepared EXECUTE then passed a nil context to context.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.

  5. slyunnn commented on Aug 24, 2026

    @slyunnn

    Regression and extended verification for #26766 (2026-08-24)

    Verification Environment

    Validated against MatrixOne origin/main commit 861a0f5ea6cf90cd278cf6d4cdb1c74234cf36039 — feat: esql_tvf / sql_tvf table functions and ENGINE = ESQL|SQL foreign external tables (#27489).

    Topology: 1 Log service + 1 TN service + 2 CN services + 1 Proxy service. The three test endpoints were Proxy 127.0.0.1:26009, CN1 127.0.0.1:26001, and CN2 127.0.0.1:26002.

    The client used Go database/sql with github.com/go-sql-driver/mysql, a single physical *sql.Conn, MySQL CLIENT_MULTI_STATEMENTS via multiStatements=true, and interpolateParams=false.

    Related fixes: #27227, commit 4d817248, fix(frontend): guard prepared multi-statement planning without a process; and the completing fix #27471, commit 48e4601fe5042f270ee404dcf16263fde69ab246, fix(frontend): restore compiler context for prepared multi-statements.

    What Was Covered

    • OK — Original reproducer: a single COM_QUERY packet containing SET @x=41; EXECUTE issue_26766_stmt USING @x completed without a panic; @a=41 and @b='before' were verified.
    • OK — Happy path: a later prepared EXECUTE in a multi-statement packet succeeded on Proxy, CN1, and CN2.
    • OK — Ordering control: SET @a=99; EXECUTE issue_26766_stmt USING @x; SET @tail=2 preserved @a=41 and @tail=2.
    • OK — Boundary/control: ordinary CLIENT_MULTI_STATEMENTS execution SET @control_a=1; SET @control_b=@control_a+1 preserved statement order.
    • OK — Unhappy path: SET @a=77; EXECUTE issue_26766_missing USING @x returned a normal SQL error, did not expose a panic, preserved @a=77, and left the same connection usable.
    • OK — Compatibility/control: standalone prepared EXECUTE and a subsequent SELECT 1 succeeded on the same physical connection.
    • Not covered — packet fragmentation, TLS, proxy failover, connection loss, or deliberate server fault injection; these are outside the compiler-context lifecycle fixed by this issue.

    Findings

    The failure was fixed by restoring the caller compiler context after synthetic planning for a preceding SET statement and by guarding the missing-process path. The original later-EXECUTE packet 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.go
    • script/14_issue_regression/issue_26766_prepared_multi_statement_protocol.sh
    • script/14_issue_regression/out/issue_26766_prepared_multi_statement_protocol.out
    • suites/14_issue_regression/t/issue_26766_prepared_multi_statement.test
    • suites/14_issue_regression/r/issue_26766_prepared_multi_statement.result

    MOTR protocol record and replay both passed against Proxy 127.0.0.1:26009. The same real-protocol replay also passed against direct CN1 127.0.0.1:26001 and CN2 127.0.0.1:26002.

    Commit: ffeb0560d9fc9fb70420b9f4377c38cb7dc028a1 (test: add MOTR regression for #26766).

    Final Conclusion

    Fixed. The original MySQL CLIENT_MULTI_STATEMENTS reproducer, extended ordering and error/reuse controls, and three-endpoint topology verification all passed on the latest origin/main. A large-data regression is not needed because data scale is unrelated to this compiler-context and protocol-statement-order defect.

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions