Skip to content

[Bug]: Python SDK session rollback leaves a successful CLONE table committed after a later CLONE fails #31235

Closed

Description

@Ariznawlll

Description

The Python SDK documents with client.session() as an atomic transaction for its clone manager, but a successful session.clone.clone_table() remains committed when a later operation in that block fails and the context manager rolls back. Sending an explicit SQL BEGIN on the same SDK session restores rollback behavior, and a native one-connection SQL BEGIN/CLONE/ROLLBACK also removes the clone. Therefore the session API's logical SQLAlchemy begin() is not enough to establish the backend transaction semantics needed for CLONE DDL.

Environment

  • Official MatrixOne main and editable Python SDK: 5fe08a3307e246e1ab9951ab690b50557af0f509
  • Python 3.11, SDK 0.1.16, SQLAlchemy 2.0.55; isolated local 1 LOG / 1 TN / 1 CN
  • SELECT VERSION() = 8.0.30-MatrixOne-v1.3.0

Steps to reproduce

from matrixone import Client

c = Client(sql_log_mode="off")
c.connect(host="127.0.0.1", port=36601, database="mo_catalog", on_connect=[])
c.execute("CREATE DATABASE clone_tx_db")
c.execute("CREATE TABLE clone_tx_db.src(id INT PRIMARY KEY)")
c.execute("INSERT INTO clone_tx_db.src VALUES(1)")

try:
    with c.session() as s:
        s.clone.clone_table("clone_tx_db.first", "clone_tx_db.src")
        s.clone.clone_table("clone_tx_db.second", "clone_tx_db.missing_src")
except Exception as e:
    print(type(e).__name__)  # CloneError: missing source

print(c.execute("SHOW TABLES FROM clone_tx_db LIKE 'first'").fetchall())
# [('first',)] -- first clone remained after rollback
print(c.execute("SHOW TABLES FROM clone_tx_db LIKE 'second'").fetchall())
# []

Actual behavior

The second clone fails as expected, but the first clone persists despite the SDK session's documented rollback. In a mixed DML/clone case, a prior INSERT is rolled back while the clone table remains, so a caller can observe a partially applied workflow.

Expected behavior

The session API explicitly promises that clone-manager operations in one context commit or rollback together. On a later exception, the first clone should be removed, as it is when the backend sees an explicit SQL BEGIN. The source table must remain unchanged and the connection usable.

Stability and controls

  • Real second-clone failure: 3/3 fresh databases leave the first SDK clone persisted, the second absent, the source intact and SELECT 42 working.
  • Same SDK route with s.execute("BEGIN") before the two clone calls: 3/3 first clone is rolled back after the second error; source intact and connection usable.
  • Asynchronous AsyncClient.session() has the same outcome on another 3/3 fresh databases: first clone persists after the second fails; await s.execute("BEGIN") removes it on rollback in 3/3 controls.
  • Mixed DML/clone route: 3/3 SDK sessions roll back INSERT but leave the clone table; adding explicit SQL BEGIN rolls back both.
  • Native MySQL-protocol one-connection BEGIN; CREATE TABLE dst CLONE src; ROLLBACK; SHOW TABLES ... control removes dst on the same server.
  • All test data was confined to uniquely named isolated databases. Failure did not corrupt the source or disconnect the client. Test-only objects can be removed with DROP DATABASE after evidence review.

White-box location

clients/python/matrixone/client.py:1707-1733 promises atomic clone-manager operations; lines 1925-1947 call SQLAlchemy Session.begin(), yield, then commit()/rollback() on exception. This starts a SQLAlchemy logical transaction but does not visibly emit a server BEGIN statement. clients/python/matrixone/clone.py also documents multi-clone atomicity in its session example. Direct s.execute("BEGIN") is the behavioral discriminator. The exact backend/driver transaction boundary should be confirmed in a fix; do not treat this source hypothesis alone as proof.

Evidence and related issues

  • Runnable probes: /private/tmp/mo-sdk-lifecycle-20261026/session_two_clone_failure_probe.py, async_session_two_clone_failure_probe.py, session_rollback_discriminator.py.
  • Searched open/closed MatrixOne issues and mysql-compatibility-contract; no matching SDK session lifecycle-DDL rollback issue found. Closed [Bug]: CREATE DATABASE ... CLONE inside an explicit transaction ignores uncommitted source state #27055 concerns database-clone read-your-own-writes inside an explicit backend transaction, a different failure mode.
  • Scope note: CREATE SNAPSHOT inside an explicit backend BEGIN is rejected by this server with 20101 unclassified statement appears in uncommitted transaction. Snapshot transactional support therefore needs a separate contract decision; this report is limited to CLONE, which demonstrably supports explicit-transaction rollback.

Regression coverage

Add SDK online tests with two lifecycle DDL operations in client.session(): normal commit and second-operation failure; assert exact target catalog/table state after rollback, source rows, retry and connection reuse. Compare with explicit backend BEGIN and cover sync/async session APIs where supported.

Activity

  1. added this to the MO-202610 milestone on Oct 26, 2026
  2. added
    kind/bug
    on Oct 26, 2026
  3. added theissue type on Oct 26, 2026
  4. added a commit that references this issue on Oct 26, 2026
    28e8760
  5. XuPeng-SH commented on Oct 26, 2026

    @XuPeng-SH
    Contributor

    Local SDK QA (2026-10-26): SDK source 28e8760905a7f39dba2ce436357ea75492f30512 passed tests/online/test_sdk_manager_contracts.py::test_session_clone_commit_rollback_and_reuse[sync] and [async] against isolated service binary 83c1e23526fc0ed31188abd7fc2ca87cd3a6d4b3 (SHA-256 6bebe05311ae6438967a824641e3849fbbc9af298003177351794601edf14b4b). In each API, a successful clone followed by a missing-source clone raised CloneError; after session rollback the first clone target was absent, the preceding insert was rolled back (source stayed at row 1), and SELECT 42 succeeded. This is the focused CLONE lifecycle contract; snapshot DDL transaction support remains outside this test.

  6. Ariznawlll commented on Oct 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 官方 main:420786e21566e5b9d5875e7d6a7f3102fd0761af;fix(python-sdk): unify query contracts and atomic manager operations #31262 已 MERGED,merge commit 28e8760905a7f39dba2ce436357ea75492f30512 是此 main 的祖先。
    • 隔离部署:从该 SHA 重建的本地 1 LOG / 1 TN / 2 CN,SQL 47011/47012;Python 3.12、SQLAlchemy 2.0.55、同工作树 SDK;二进制 SHA-256 8bd0f1f4c60cd0590c68cdbb2cbca31fc9d39d071ec367ab56d5c49b4eec327e。
    • 测试日期:2026-10-27。

    原问题验证

    同步/异步 session 先验证一次合法 CLONE 能提交,再进入新事务:插入源 id2、成功 CLONE 目标、随后对不存在源再 CLONE 抛 CloneError。退出事务后目标表不存在,源表仍仅有 id1,SELECT 42=42;各入口三轮通过。

    Happy Path 覆盖

    1. 合法单独 CLONE 先成功,说明失败事务的回滚不是因为 CLONE 本身不可用。
    2. 四个 SDK 在线合同文件连续三轮,每轮 70/70;112 项定向离线合同也连续三轮。两类均零失败、错误、跳过。

    Unhappy Path 覆盖

    1. 异步 CLONE 已执行后取消 session 的定向测试另外 10/10:目标表不存在、源仅 id1、池 checked-out 为 0、连接可继续 SELECT 42。

    白盒验证

    本轮核对已合入 SDK 合同源码中的公开 API、精确值及失败后状态断言,实际运行在上述 main 服务;未修改产品源码、未执行全量 SDK suite/race。

    回归测试

    #31262 已包含 clients/python/tests/online/test_sdk_manager_contracts.py::test_session_clone_commit_rollback_and_reuse[sync/async],clients/python/tests/online/test_sdk_search_contracts.py::test_cancelled_clone_session_rolls_back。SDK API 语义不能以纯 SQL MOTR 取代,本轮未新增 MO 或 MOTR PR。#31262 六个离线 SDK CI job 成功、在线 CI SKIPPED;本地在线测试另有记录。

    测试证据

    JUnit:/private/tmp/mo-six-qa-20261026/qa-night/sdk-contract-round-{1,2,3}.xml(每轮 70、失败/错误/跳过均 0),sdk-offline-round-{1,2,3}.xml(每轮 112、失败/错误/跳过均 0)。取消时序:sdk-timing-round-{1..10}.xml,每轮 4/4。修复 PR:#31262 。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions