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

Ouverte Adaptée aux débutants
#1,691 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 6 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
74/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
mysql, postgresql, python
Domaine
database

Piste de recherche

Commence dans src/datajoint/table.py à Table.delete(), en lisant le bloc qui ouvre la transaction, l'invite user_choice("Commit deletes?") et le try/except Exception existant qui appelle cancel_transaction(). Reproduis avec l'extrait builtins.input patché par mock, puis élargis le gestionnaire pour que KeyboardInterrupt et EOFError fassent aussi un rollback lorsque transaction est encore True, en re-levant ensuite. Terminé signifie que conn.in_transaction est False après l'interruption et que les tests de delete existants passent toujours.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Python
Étoiles
198
Forks
103
Merge moyen
6 j 1 h
PR mergées (30 j)
10

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de datajoint/datajoint-python

Toutes les issues de datajoint/datajoint-python

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.