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

Đang mở Phù hợp với người mới
#1,691 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 6 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
74/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
mysql, postgresql, python
Lĩnh vực
database

Hướng nghiên cứu

Bắt đầu trong src/datajoint/table.py tại Table.delete(), đọc khối mở giao dịch, lời nhắc user_choice("Commit deletes?"), và try/except Exception hiện có gọi cancel_transaction(). Tái hiện bằng đoạn builtins.input được patch bằng mock, sau đó mở rộng trình xử lý để KeyboardInterrupt và EOFError cũng rollback khi transaction vẫn là True, rồi re-raise sau đó. Hoàn tất nghĩa là conn.in_transaction là False sau khi bị ngắt và các test delete hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
198
Fork
103
Merge trung bình
6 ngày 1 giờ
Pull request đã merge (30 ngày)
10

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của datajoint/datajoint-python

Tất cả issue của datajoint/datajoint-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.