[Bug]: Python SDK Session Stage helpers escape transaction: LOAD survives rollback and EXPORT misses uncommitted rows

Open
#33,310 1 comment 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@XuPeng-SH is already working on this.

Since Nov 26, 2026.

  • #33326 by @MatrixAdventurer — closed without merging

Assessment

This issue has not been assessed yet.

Description

kind/bug
Is there an existing issue for the same bug?
  • I have checked the existing issues.
Version Information
  • matrixone-python-sdk 0.1.16, source checkout from main c0cef5c9cce2164fedd201c188bfffe4d2d09a96 (clients/python)
  • Python 3.12.3, SQLAlchemy 2.0.43, PyMySQL distribution 1.1.2, aiomysql 0.3.2
  • Server: self-built MatrixOne main at the same commit; version() = 8.0.30-MatrixOne-v1.3.0, git_version() = c0cef5c
Steps to Reproduce

A Stage obtained through session.stage.get() does not retain the Session executor. This causes two transaction-visibility failures: LOAD survives Session rollback, and EXPORT cannot see the Session's uncommitted writes.

Minimal synchronous reproduction (the async equivalents are described below):

import csv
import tempfile
from pathlib import Path

from matrixone import Client


class RollbackProbe(Exception):
    pass


client = Client(sql_log_mode="off")
client.connect(
    host="127.0.0.1", port=6001,
    user="root", password="111", database="sdk_test",
)
client.execute("DROP TABLE IF EXISTS stage_tx_repro")
client.execute("CREATE TABLE stage_tx_repro (id INT PRIMARY KEY, name VARCHAR(30))")

with tempfile.TemporaryDirectory() as td:
    client.stage.create("stage_tx_repro", f"file://{td}/")
    Path(td, "rows.csv").write_text("1,alice\n2,bob\n", encoding="utf-8")

    # 1. LOAD through the Stage object returned by the Session.
    try:
        with client.session() as session:
            session.stage.get("stage_tx_repro").load_csv("rows.csv", "stage_tx_repro")
            raise RollbackProbe()  # rolls back the Session
    except RollbackProbe:
        pass

    count = client.execute("SELECT COUNT(*) FROM stage_tx_repro").fetchall()[0][0]
    print("rows after rollback:", count)  # actual: 2; expected: 0

    # 2. EXPORT through the Stage object returned by the Session.
    client.execute("DELETE FROM stage_tx_repro")
    client.execute("INSERT INTO stage_tx_repro VALUES (1, 'committed')")

    try:
        with client.session() as session:
            session.execute("INSERT INTO stage_tx_repro VALUES (2, 'uncommitted')")
            session.stage.get("stage_tx_repro").export_to_csv(
                "out.csv", "SELECT id FROM stage_tx_repro ORDER BY id"
            )
            with Path(td, "out.csv").open(newline="", encoding="utf-8") as stream:
                ids = [int(row[0]) for row in csv.reader(stream) if row and row[0] != "id"]
            print("exported ids:", ids)  # actual: [1]; expected: [1, 2]
            raise RollbackProbe()
    except RollbackProbe:
        pass

The direct Session managers are positive controls:

  • Replacing the Stage LOAD with session.load_data.read_csv_stage(...) leaves 0 rows after rollback.
  • Replacing the Stage EXPORT with session.export.to_csv_stage(...) exports [1, 2], including the uncommitted row.

Async has the same defect:

async with async_client.session() as session:
    stage = await session.stage.get("stage_tx_repro")
    await stage.load_csv("rows.csv", "stage_tx_repro")
    # After Session rollback: 2 rows remain; direct async Session manager leaves 0.

async with async_client.session() as session:
    await session.execute("INSERT INTO stage_tx_repro VALUES (2, 'uncommitted')")
    stage = await session.stage.get("stage_tx_repro")
    await stage.export_to_csv("out.csv", "SELECT id FROM stage_tx_repro ORDER BY id")
    # Exports [1]; direct async Session manager exports [1, 2].

Each LOAD and EXPORT reproduction was run in two fresh Python processes with unique databases, stages, and tables. Results were identical in both rounds.

Actual Behavior
Execution path Rows remaining after LOAD + rollback IDs exported before rollback
Native PyMySQL, one connection 0 [1, 10]
Direct sync Session manager 0 [1, 20]
Stage object from sync Session 2 [1] (expected [1, 30])
Direct async Session manager 0 [1, 40]
Stage object from async Session 2 [1] (expected [1, 50])

For EXPORT, rolling back the Session afterwards leaves only the originally committed row in the base table. Thus the missing row in the CSV is not evidence that the INSERT was committed or visible globally; the Stage helper read through a different connection/transaction.

The native controls use the corresponding server SQL on one PyMySQL connection:

  • LOAD DATA INFILE 'stage://...' followed by ROLLBACK leaves 0 rows.
  • An uncommitted INSERT followed by SELECT ... INTO OUTFILE 'stage://...' exports both the committed and uncommitted rows.
Expected Behavior

A Stage object returned by a Session-bound StageManager should operate on that Session's executor:

  1. load_csv() must join the current transaction, so Session rollback removes the loaded rows.
  2. export_to_csv() must use the current transaction's visibility and include its uncommitted writes.
  3. Sync and async should have the same semantics as their direct Session managers and the native single-connection controls.
Additional information

The ownership loss is visible in clients/python/matrixone/stage.py:

  • BaseStageManager._process_get_result() constructs Stage(..., _client=self.client) and does not propagate self.executor.
  • Stage.load_csv() constructs LoadDataManager(self._client).
  • Stage.export_to_csv() constructs ExportManager(self._client).

Consequently, these helpers fall back to Client-managed pooled/autocommit connections instead of the Session connection. The Stage object should retain the owning executor and pass it to the load/export managers; Client-owned Stage objects should retain their existing behavior.

Other Stage load/export convenience methods share the same ownership pattern, but only load_csv() and export_to_csv() are claimed as reproduced here. After the error/rollback paths, both sync and async clients remained usable (SELECT 42 succeeded).

Dominant language
Go
Stars
1.9k
Forks
312
Avg merge
22h 59m
Merged PRs (30d)
736

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matrixorigin/matrixone

All issues in matrixorigin/matrixone

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.