Skip to content

[Bug]: Failed re-PREPARE keeps the previous statement instead of deallocating it #25977

Closed

Description

@Ariznawlll

Summary

When a prepared statement name is reused and the new PREPARE fails, MatrixOne keeps the previous statement under that name. A later EXECUTE therefore runs stale SQL.

MySQL deallocates the previous statement before attempting the new prepare; after the new prepare fails, no statement remains under that name.

Tested version

main at 3046f14d94b3f1a25839eeeb65729b64f85ac647.

Reproduction

Run the following in one session:

USE join_main_audit;
PREPARE s FROM 'SELECT 1';

SET @bad = 'SELECT * FROM prepare_missing_table';
PREPARE s FROM @bad;

EXECUTE s;

Actual result

The second PREPARE correctly reports that prepare_missing_table does not exist, but EXECUTE s still runs the old statement and returns:

1

This reproduces 3/3 times.

Expected result

The previous statement should be deallocated before the replacement is prepared. Since the replacement fails, EXECUTE s should report that the prepared statement does not exist.

MySQL 8.0.45 returns:

ERROR 1243 (HY000): Unknown prepared statement handler (s) given to EXECUTE

Root cause

The frontend builds the replacement object before calling Session.SetPrepareStmt. The old object is only closed inside SetPrepareStmt, so any parse, authorization, or planning error before that call leaves the old statement in ses.prepareStmts.

This also creates a stale-statement risk: a client may assume the failed replacement invalidated the old statement, while MatrixOne continues to execute the previous SQL.

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 回归结果(2026-07-22)

    • 测试基线:c0486a2446c84c6dd6bf142494fe85243877ddc5
    • 先创建返回 77 的同名语句,再用语法错误 SQL 重新 PREPARE。
    • 实际结果:第二次 PREPARE 报错后,EXECUTE 仍返回旧值 77。
    • 结论:旧语句仍被保留,问题仍可复现。
  5. added
    severity/s0
    and removed on Jul 28, 2026
  6. added a commit that references this issue on Jul 28, 2026
    0c8b450
  7. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

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

    原问题验证

    每轮先 PREPARE stale FROM 'SELECT 1',再以相同名称执行失败 replacement。随后 EXECUTE stale 必须失败,而不是继续执行旧 SELECT 1;最后同名 PREPARE stale FROM 'SELECT 2' 与 EXECUTE 必须恢复成功。完整用例重复 3 次。

    Happy Path 覆盖

    1. 初始 prepared SELECT 正常创建。
    2. 失败 replacement 之后,使用同名有效 SQL 重新 PREPARE,读取结果稳定为 2。
    3. 每轮显式 DEALLOCATE,防止 statement registry 泄漏影响下一轮。

    Unhappy Path 覆盖

    1. replacement 指向不存在的表:PREPARE 失败后旧句柄不可 EXECUTE。
    2. replacement 为 SELECT FROM 语法错误:同样不会遗留可执行的旧句柄。
    3. 两种失败后同名恢复均成功,证明失败状态未永久污染 session registry。

    白盒验证

    本轮覆盖公共 statement-name 生命周期;未注入内部 registry 故障。关键断言是失败 re-PREPARE 后旧 name 不可执行,而非仅检查 replacement 返回 error。

    回归测试

    该稳定 SQL session 状态机适合 prepared BVT;若需要同一 socket 的 protocol handle 覆盖,可补 motr/suites/14_issue_regression。本轮未新提交。

    测试证据

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

    三轮均通过。

    测试结论

    测试通过。

  8. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    motr 回归补充

    • 测试版本:main@c726a6199094184a2e6ebc7504c3e011982b8976
    • 拓扑:本地单 LOG/TN/CN,经 127.0.0.1:6001;2026-08-04。
    • 回归 PR:motr#74,提交 3d79e01b6348e23bb747de5ab9a9d16995304a29。

    新增 suites/14_issue_regression/t/issue_25977_failed_reprepare_deallocates.test:

    1. 先成功创建同名 prepared handle。
    2. 用缺表 SQL 和语法错误两种方式覆盖同名 PREPARE 失败;随后执行旧名称均返回 20400 invalid state prepared statement ... does not exist,证明旧 handle 已被释放、没有继续指向旧计划。
    3. 每种失败后分别更正 SQL、以同名 handle 重建并读回 2 / 3,验证失败不会把 session 或名称空间永久污染。

    执行证据:

    ./motr --config /private/tmp/motr-prepared-session-16031.yml --repeat 3 \
      14_issue_regression.issue_25977_failed_reprepare_deallocates
    
    3/3 success;每轮 19 个断言;完整 14_issue_regression suite 中同样 success。
    

    该问题属于 SQL-level named PREPARE 生命周期,公开 SQL 已覆盖失败原子性与恢复,不额外采用白盒注入或 binary-protocol 场景。

  9. added 3 commits that reference this issue on Sep 17, 2026
    7f39cbd
    6144f3d
    5aba027
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions