Skip to content

[Bug]: Prepared multi-assignment SET panics and partially updates session variables #26685

Closed

Description

@Ariznawlll

Description

A text-protocol prepared statement containing more than one user-variable
assignment panics while evaluating the second assignment. The failure is also
non-atomic: the first assignment has already changed session state before the
server returns the error.

测试版本

  • 分支:main
  • commit:c726a6199094184a2e6ebc7504c3e011982b8976
  • 部署方式:本地隔离单节点 LOG / TN / CN 集群,以 mysql text protocol 执行 SQL PREPARE/EXECUTE
  • 测试日期:2026-08-04

原问题验证

以下最小 SQL 在三个独立新连接中均复现:

SET @a = 99, @b = 'before';
PREPARE multi FROM 'SET @a = ?, @b = ?';
SET @x = 41, @y = 'first';
EXECUTE multi USING @x, @y;
SELECT @a, @b;

每次 EXECUTE 都返回 ERROR 20101 (HY000): internal error: panic runtime error: invalid memory address or nil pointer dereference。panic 栈落在 (*Process).GetPrepareParamsAt,由 ParamExpressionExecutor.Eval 调用。失败后查询得到 41, before,即第一项已更新、第二项没有更新。

Happy Path 覆盖

  1. 直接 SET @a=41,@b='first' 成功,读取结果为 41, first。
  2. 同一集群中已覆盖的单赋值 prepared SET:literal SET @x=41+1、parameterized SET @x=?、binary protocol 的单赋值及参数替换均成功。
  3. panic 返回后连接仍可执行 SELECT 1;该可用性检查不改变前述部分写入事实。

Unhappy Path 覆盖

  1. SET @a=?,@b=?:3/3 返回 nil-pointer panic。
  2. SET @a=?+1,@b=?:3/3 返回同一 panic,说明表达式不是触发前提。
  3. 失败原子性:3/3 首个目标变量被改写为 41,第二个仍为原值 before。

白盒验证

  • pkg/frontend/mysql_cmd_executor.go:doSetVar 逐项调用 getExprValueWithPrepareMode。
  • 每项都会进入 pkg/frontend/back_exec.go:executeStmtInSameSession 的派生 SELECT 路径;清理时对当前 process 调用 Free()。
  • Process.Free() 会清空 prepare 参数;随后第二项的 ParamExpressionExecutor.Eval 调 GetPrepareParamsAt,该函数直接访问 proc.Base.prepareParams.Length(),在参数已被清空时发生 nil dereference。
  • 现有 TestPreparedSetBinaryProtocolReportsAndReplacesParameters 与 TestPreparedSetExpressionParamsAfterInit 在本提交上通过,但只覆盖参数报文/初始化,不覆盖多 assignment 的最终 EXECUTE。命令:
go test ./pkg/frontend -run '^(TestPreparedSetBinaryProtocolReportsAndReplacesParameters|TestPreparedSetExpressionParamsAfterInit)$' -count=1 -v

回归测试

修复后应新增真实 SQL 回归:text protocol 和 binary protocol 各覆盖 SET @a=?,@b=?、SET @a=?+1,@b=?、重复执行、NULL 重绑、失败后两变量均保持旧值。稳定 SQL 可放入 prepared BVT;协议与同一连接的状态原子性可放入 motr/suites/14_issue_regression 的 scenario driver。

测试证据

本地 live 回归命令:

go test ./pkg/tests/issues -run '^TestCodexLive25996PreparedMultiSetPanicIsConnectionSafe$' -count=3 -v

三次均得到 @a=41,@b=before,且三个重复的最小 mysql-client SQL 均返回 ERROR 20101。关联来源 issue #25996(其修复 PR #26096)。

测试结论

测试不通过。

Activity

  1. added this to the MO-202608 milestone on Aug 4, 2026
  2. added
    kind/bug
    on Aug 4, 2026
  3. added theissue type on Aug 4, 2026
  4. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归基线处理补充

    本 issue 的 multi-assignment SET panic / partial mutation 在 main@c726a6199094184a2e6ebc7504c3e011982b8976 上仍是 3/3 复现。正常控制为单项文本 SET 可完成;失败输入是多项 marker 赋值,异常前已能观察到部分 session 状态变更,故不满足失败原子性。

    因此未把该失败写入 motr#74 的 .result。修复后请补 protocol/text 两条回归:单项、多项、NULL 和异构类型重绑;每个拒绝路径后读取所有变量并复用同一连接,断言零部分修改、无 panic。

    测试不通过。

  5. VioletQwQ-0 commented on Aug 6, 2026

    @VioletQwQ-0
    Collaborator

    QA handoff for merged PR #26705

    PR and merge:

    QA decision:

    • Required: yes. This is user-visible prepared SQL behavior with a recovered panic and session-state failure-atomicity risk.
    • Automated evidence: exact-head focused text PREPARE/EXECUTE and binary COM_STMT_PREPARE/EXECUTE regressions passed, including rebinding/NULL, left-to-right visibility, repeated/self-referential targets, late RHS rollback, reserved user-variable names, mixed system-variable rejection, connection reuse, and parameter ownership cleanup. The canonical prepare BVT passed locally, and the merged PR required checks passed for standalone BVT, UT, SCA, Compose/Proxy, coverage, MongoDB, and Iceberg. The post-merge TKE BVT check is still pending at handoff.
    • Remaining validation: on a deployed post-merge build, verify text and binary drivers across repeated rebinding, NULL and heterogeneous values, repeated/self-referential targets, later-RHS failure, invalid arity, mixed system-variable rejection, unchanged final session state, and connection reuse.
    • Expected result: PASS means no panic, left-to-right visibility is preserved, and every rejected/failing statement leaves all session variables unchanged. A FAIL must include the tested version, environment, and terminal result and keep this issue open.

    Tester selection: the issue author is Ariznawlll; under the automation rule, non-heni02 issue authors are assigned to Ariznawlll.

  6. Ariznawlll commented on Aug 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d0057b3503611022cdf9a6e24c98065d209eb8e4
    • 部署方式:本地隔离双 CN(CN1 / CN2),MySQL text protocol 与 Go MySQL driver binary protocol。
    • 测试日期:2026-08-07

    原问题验证

    在 CN1 → CN2 → CN1 的 3 个新连接中重复执行原始多赋值 prepared SET:

    set @a = 99, @b = 'before';
    prepare p from 'set @a = ?, @b = ?';
    set @x = 41, @y = 'first';
    execute p using @x, @y;
    select @a, @b;

    每轮 EXECUTE 都成功,结果均为 41, first;未再出现原问题中的内部空指针错误,也没有部分变量更新。

    Happy Path 覆盖

    1. text protocol:两项 marker 赋值、? + 1 和字符串 concat 均正确;首次执行得到 42, first-text。
    2. 同一 text handle 重绑 9, NULL 后得到 10, NULL,确认参数重用与 NULL 值正确。
    3. 顺序可见性:set @visibility_a=?, @visibility_b=@visibility_a+1 得到 41,42;同一目标变量重复赋值 set @self_ref=?, @self_ref=@self_ref+1 得到 42。
    4. binary protocol:Go driver PrepareContext / ExecContext 的两项赋值及 NULL 重绑与 text protocol 结果一致。
    5. 每轮结束后复用同一连接执行 select 1,均返回 1。

    Unhappy Path 覆盖

    1. 参数数目不足:execute p using @x 正确返回 Incorrect arguments to EXECUTE;预置变量 77, stable 保持不变。
    2. 后段 RHS 返回多行:首项赋值后再计算 (select v from multi_set_values) 正确返回 Subquery returns more than 1 row;两个目标变量均保持执行前值 88, unchanged。
    3. 混合 user/system variable:set @a=?, transaction_isolation=? 正确拒绝,并保持 @a 和 @@transaction_isolation 不变。
    4. 上述三种拒绝路径后都在同一物理连接执行 select 1,均成功;未观察到连接状态损坏。

    白盒验证

    go test ./pkg/tests/issues -run '^TestIssue26685PreparedMultiSet$' -count=3 -v
    

    text_protocol 与 binary_protocol 子路径连续 3 次通过。该用例覆盖重复/self-reference 赋值、后段表达式失败回滚、保留变量名、NULL/异构重绑和 mixed system variable 拒绝。

    回归测试

    当前 main 已包含 pkg/tests/issues/issue_26685_test.go,并在 test/distributed/cases/prepare/prepare_all.sql 中保留 text BVT 覆盖。本轮为修复后的验证,未额外修改回归文件。

    测试证据

    live 双 CN 验证命令:

    go run /tmp/prepared_live_validation_d0057b35.go
    # prepared_validation_passed run=1 port=58613
    # prepared_validation_passed run=2 port=58614
    # prepared_validation_passed run=3 port=58613
    

    另发现一条不同的 multi-statement 包顺序缺陷,已独立记录为 #26766;它不影响本 issue 所覆盖的单条 prepared EXECUTE 路径。

    测试结论

    测试通过。

  7. Ariznawlll commented on Aug 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归测试补充

    已新建 motr#83,新增 issue_26685_prepared_multi_set 协议回归。

    • text prepared multi-SET:参数重绑、NULL、前项赋值对后项可见;
    • binary prepared multi-SET:参数重绑、NULL、参数不足后的状态不变及 statement 复用;
    • text 拒绝路径:参数不足、后段 RHS 子查询多行错误后,两个目标变量均保持原值;
    • 同连接 SELECT 1 复用校验。

    本地 motr runner 在 CN1、CN2 各连续 3 次通过。#26602 的 SQL/binary 回归已经在 motr main,本 PR 未重复提交其文件。

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions