delete(): Ctrl-C or end of input during a delete leaves its transaction open
維護者通常 6 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 74/100
研究方向
從 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 內容生成。
描述
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 setstransaction=Falsewhen 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 explicitconn.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 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
datajoint/datajoint-python 的其他 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 78/100
datajoint/datajoint-python#1703 ·
維護者通常 6 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 78/100
datajoint/datajoint-python#1702 ·
維護者通常 6 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 78/100
datajoint/datajoint-python#1701 ·
維護者通常 6 天內回覆
-
dependencies needs-discussion
難度 2/5 1-3 小時 新手友好度 68/100
datajoint/datajoint-python#1687 ·
維護者通常 6 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 80/100
datajoint/datajoint-python#1668 ·
維護者通常 6 天內回覆
查看 datajoint/datajoint-python 的全部 Issue
相似的 Issue
-
cognition
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 78/100
collabnix/awesome-mcp-lists#433 ·
維護者通常 4 天內回覆
-
CrossEntropyFilter raises on zero probabilities and single-class results可能已有人在做 @NopeCharon 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
Archmonger/ServeStatic#129 ·
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 2 天內回覆