Skip to content

[Bug]: COM_STMT_CLOSE on an absent statement sends a response and desynchronizes the next command #27338

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I searched open and closed issues and fix PRs.

Related but not duplicates:

Branch Name

main

Commit ID

71031d0e9e45920821d71ce03f519181d346e669

Other Environment Information

  • MatrixOne was built from the commit above.
  • MySQL control: 8.0.46.
  • JDBC clients: MySQL Connector/J 8.0.33 and 26.7.0.
  • The deterministic protocol probe was repeated 3 times per endpoint on fresh connections.

Actual Behavior

MatrixOne sends an error response for COM_STMT_CLOSE when the statement id is absent. The classic MySQL protocol defines COM_STMT_CLOSE as a no-response command, including the absent-id path.

This leaves an unexpected packet in the response stream because clients do not read a response for this command. A following command may consume that packet instead of its own response.

Deterministic two-command probe on a fresh connection:

  1. Send COM_STMT_CLOSE with statement id 0.
  2. Send COM_PING without reading between the two commands.
  3. Read the first response packet.

Results, 3/3:

MatrixOne: first packet = ERR, code 20400
MySQL 8.0.46: first packet = OK for COM_PING

Every fresh MatrixOne connection still prepares and executes SELECT ? successfully, so the effect is isolated to the connection whose packet stream was shifted.

JDBC impact observed with Connector/J 26.7.0:

  • A server-side prepareStatement(...) fails, for example because the SQL references an absent object.
  • Connector/J cleans up its half-constructed server statement with COM_STMT_CLOSE and does not expect a response.
  • A following ordinary or prepared statement can intermittently receive HY000 / 20400, reporting that internal prepared statement __mo_stmt_id_0 does not exist.
  • Connector/J 8.0.33 did not take the same cleanup path in this matrix; MySQL remained synchronized with both versions.

Expected Behavior

COM_STMT_CLOSE must not send a response packet. An absent statement id should be handled silently on this binary command, while text SQL DEALLOCATE PREPARE unknown_name may continue returning MySQL error 1243.

MySQL's protocol source documents:

COM_STMT_CLOSE deallocates a prepared statement. No response packet is sent back to the client. Return: None.

The MySQL server's absent-id branch for COM_STMT_CLOSE also goes to silent_error.

Primary references:

Root Cause

In pkg/frontend/mysql_cmd_executor.go, the COM_STMT_CLOSE branch calls Session.GetPrepareStmt. When the id is absent it returns a GeneralErrorResponse. The frontend then writes that error packet even though this protocol command must not produce any response.

The existing unit expectation for a missing binary close also treats an error response as correct, so the protocol mismatch is currently encoded in the test.

Recovery and Consistency Checks

  • Reproduced on the latest official main commit above.
  • MatrixOne result was identical in 3/3 deterministic runs.
  • MySQL control was identical in 3/3 runs and did not emit a close response.
  • A fresh connection remained usable after every run.
  • The probe performs no table writes, so there is no partial-data outcome.

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    Latest-main recheck after main advanced:

    • MatrixOne: 493ab8a9585576b76508d054fef58ccb55c206c5
    • The only intervening source change is for LAST_QUERY_ID; the frontend protocol path is unchanged.
    • Deterministic COM_STMT_CLOSE(id=0) -> COM_PING probe: MatrixOne returned the extra ERR/20400 packet in 3/3 runs.
    • MySQL 8.0.46 returned only the COM_PING OK packet in 3/3 runs.
    • A fresh server-side PreparedStatement succeeded after every run.

    The issue remains reproducible on the exact current origin/main.

  4. added theissue type on Sep 23, 2026
  5. XuPeng-SH commented on Oct 15, 2026

    @XuPeng-SH
    Contributor

    Reconfirmed on 2026-10-15 through an ordinary Python DB-API error/reuse path (no hand-built packets), on both:

    • Official v4.2.5 1a44f3db3056e32975fde86ca0ab69a094996a3f, executable SHA256 5d136df6fab5e7a5bdf4ba3e678470d898c8d681f3fdae99095ecd9c4205d2ed.
    • Freshly fetched, built and actually executed main 14686eb61cc510ff456951b946e8e33496806320, executable SHA256 a9fe1d1f71eed4c714d28abfde2e19bdf5ff8be2877b0e65bc5d154205c5eed2. Closing main-tip check still matched this SHA. Production source unchanged.
    • mysql-connector-python==26.7.0, use_pure=True, independent task-owned local services.

    Minimal normal-client sequence on a fresh connection:

    p = connection.cursor(prepared=True)
    t = connection.cursor()
    p.execute('SELECT ? AS first_value', (11,)); p.fetchall()
    try:
        p.execute('SELEC ?', (22,))  # intentional syntax error,1064
    except mysql.connector.Error:
        pass
    t.execute('SELECT 33 AS text_before'); assert t.fetchall() == [(33,)]
    p.execute('SELECT ? AS subsequent_value', (44,))  #20400, missing __mo_stmt_id_1

    This reproduced 3/3 fresh connections per version. A following SELECT 55 AS text_after returned an empty result instead of55 in all affected trials, consistent with a shifted response stream; this is not an independent SQL-result/ACL defect. Valid reprepare controls and replacing the failed prepared cursor with a new cursor both returned the expected scalar values3/3 per version. Release returns strings for the bare prepared parameter whereas main returns integers; these raw metadata/type observations were retained and not conflated with the synchronization error.

    Tracing only the connector's public connection-method calls shows its failed-PREPARE path retains the old, already-closed statement handle; subsequent reuse calls cmd_stmt_close for that old id before the next prepare. In this main source, the absent-handle close branch still returns NewGeneralErrorResponse, and the routine writes non-nil responses. This is the existing no-response contract defect tracked here, now reached by ordinary cursor error handling. No explicit duplicate-close command was sent by the test program.

    This path initially contaminated a shared query/EXPLAIN sampling connection during hybrid fulltext/vector testing. With plan sampling isolated on its own connections, the main180s write/read/live-IVF-rebuild profile and independent restart audits passed. The contaminated downstream assertions are not being filed as separate ACL or ANN bugs. These SELECT-only cursor probes do not establish a service crash, data loss, or the cause of any restart-recovery incident.

  6. added
    severity/s0
    and removed on Oct 15, 2026
  7. XuPeng-SH commented on Oct 15, 2026

    @XuPeng-SH
    Contributor
  8. added a commit that references this issue on Oct 28, 2026
    1f18a9f
  9. XuPeng-SH commented on Oct 28, 2026

    @XuPeng-SH
    Contributor

    QA status for merged main f97056d1107a89854bfac2c06202698e3aafd41e (#31413): no current-head frontend or binary-protocol test has been run in this environment. Available memory/disk headroom under the existing guard is insufficient for a safe frontend link or current-head service. The original missing/known/repeated COM_STMT_CLOSE followed by PING and ordinary prepared-cursor error/reuse path remain pending independent verification; no product failure is inferred from the resource limitation. Handing off to QA for verification.

  10. Ariznawlll commented on Oct 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。二进制协议对不存在、正常和重复关闭的语句均不发送响应,后续命令保持对齐。

    关键证据

    • 版本:官方 main@72d99c85751dc5b9b7775cb50571e7efd6400487,包含已合入修复 PR fix(frontend): silence missing binary statement closes #31413;macOS arm64 隔离单进程 LogService / TN / CN,实际启动二进制 git_version() 为 72d99c8575。
    • 原始场景:新连接发送不存在的 COM_STMT_CLOSE(id=0) 后立即发送 COM_PING,首个响应均为 PING 的 OK,3/3;准备有效语句后关闭及再次关闭,再发送 PING 均为 OK,3/3。文本协议 DEALLOCATE PREPARE 不存在的名称仍返回 1243,随后 SELECT 42 正常。
    • mysql-connector-python 26.7.0 的原评论场景:有效预编译查询后执行故意错误的 SELEC ?,复用原游标执行参数查询得到 44,普通查询前后分别得到 33 和 55,3/3。Connector/J 26.7.0 和 8.0.33 的真实服务端预编译失败后在同一连接继续预编译查询,也分别 3/3。
    • MOTR 回归 PR #287,协议场景源码:定向 MOTR 3/3、0 跳过;该提交的 Python 3.10/3.12/3.13 离线 CI 和 Go 离线 CI 通过。未运行完整 MOTR suite;未将该结果外推到发布分支。
    • 官方 main 随后前进至 9e29011e7313a57962adc00963baa659d1e81e37,重建对应二进制后,MOTR 协议场景在新 SHA 补跑 3/3,通过且 0 跳过;此前 JDBC / Python 客户端各 3/3 结果仍对应 72d99c…。

    待解决点

    无。

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