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

オープン 初心者向け
#1,691 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

まだ誰も着手していません。

評価

難易度
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 スニペットで再現し、次にハンドラを広げて、transaction がまだ True のときに KeyboardInterrupt と EOFError もロールバックし、その後で再送出するようにしてください。完了とは、割り込み後に 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時間
マージ済み PR(30日)
10

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

datajoint/datajoint-python のほかの issue

datajoint/datajoint-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。