delete(): Ctrl-C or end of input during a delete leaves its transaction open

未關閉 適合新手
#1,691 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 6 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
74/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
mysql, postgresql, python
領域
database

研究方向

從 src/datajoint/table.py 的 Table.delete() 開始,閱讀開啟交易的程式碼區塊、user_choice("Commit deletes?") 提示,以及呼叫 cancel_transaction() 的現有 try/except Exception。用 mock 打補丁的 builtins.input 片段重現,然後擴大處理常式,使 KeyboardInterrupt 和 EOFError 在 transaction 仍為 True 時也回滾,之後再重新拋出。完成意味著中斷後 conn.in_transaction 為 False,並且現有的 delete 測試仍然通過。

由索引模型根據 Issue 內容生成。

描述

bug

Summary

Table.delete() with prompt=True runs the deletes inside its transaction and then asks "Commit deletes?" (src/datajoint/table.py, the user_choice("Commit deletes?", default="no") call). The question is asked after the try/except Exception that rolls the transaction back on errors. If the user presses Ctrl-C at the prompt (KeyboardInterrupt), or input ends (EOFError, for example a script run without a terminal), nothing rolls the transaction back, and the connection stays in it.

The same happens for a KeyboardInterrupt during the delete loop, with or without the prompt, since except Exception does not catch it.

Reproduced on master with MySQL 8.0 and PostgreSQL 15.

Consequences

On the same connection afterwards:

  • delete(prompt=True) raises "Delete cannot use a transaction within an ongoing transaction".
  • delete(prompt=False) joins the stale transaction (it sets transaction=False when one is open), deletes the rows, logs "Delete completed", and returns their count. Inserts join it too.
  • None of this is committed. It is lost when the connection closes or the transaction rolls back, including the rollback done by the next with conn.transaction: block, which raises "Nested connections are not supported." Nothing tells the user. An explicit conn.commit_transaction() would instead commit the delete the user never confirmed.
  • The transaction holds the locks taken by the deletes until it ends. Another session that writes those rows waits and then fails with a lock timeout (MySQL error 1205, PostgreSQL LockNotAvailable).

Reproduce

import datajoint as dj
from unittest import mock

schema = dj.Schema("delete_prompt_demo")

@schema
class Subject(dj.Manual):
    definition = """
    subject_id : int32
    """

Subject.insert([(1,), (2,), (3,)])
conn = Subject.connection

with mock.patch("builtins.input", side_effect=KeyboardInterrupt):
    try:
        (Subject & {"subject_id": 1}).delete(prompt=True)
    except KeyboardInterrupt:
        pass
print(conn.in_transaction)  # True: the transaction is still open

print((Subject & {"subject_id": 2}).delete(prompt=False))  # joins it: 1
conn.close()
conn.connect()
print(Subject.fetch("subject_id"))  # [1 2 3]: neither delete was committed

Fix

Roll back on any exit other than a confirmed commit: wrap the delete loop and the prompt in try/except BaseException (or finally) that calls cancel_transaction() when delete() opened the transaction and did not commit, then re-raises. transaction is still True at that point only when delete() opened the transaction itself, so it is the right guard.

主要語言
Python
星號
198
分支
103
平均合併
6 天 1 小時
30 天內合併 PR
10

環境準備

  • 提供 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

datajoint/datajoint-python 的其他 Issue

查看 datajoint/datajoint-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。