Opened 11 years ago

Closed 11 years ago

#21029 closed Bug (fixed)

Schema editors leak transaction state on exception

Reported by: Michal Petrucha Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When a schema editor is used as a context manager and an uncaught exception is raised within its block, BaseDatabaseSchemaEditor's __exit__ method doesn't call atomic.__exit__, which leaves the transaction opened when entering the block open.

I'm going to open a pull request shortly with a simple patch which does fix things for me, but I'm not sure about its correctness and I have no idea how to test this. An alternative approach might be to store an instance of Atomic as an attribute in BaseDatabaseSchemaEditor and call its __enter__ and __exit__ instead of creating separate instances; I have no idea whether that makes any difference or not.

Change History (6)

comment:1 by Aymeric Augustin, 11 years ago

I'm probably to blame for suggesting the __enter__ / __exit__ hack to Andrew, but I have to say this is very bad.

If we're using this hack, we must guarantee that __exit__ will be called with robust exception handling.

comment:2 by Michal Petrucha, 11 years ago

The pull request can be found here: https://github.com/django/django/pull/1545

comment:3 by Tim Graham, 11 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:4 by tuxcanfly, 11 years ago

Added a simple test:

https://github.com/koniiiik/django/pull/2

As expected, the test fails on master, but passes with the patch (by koniiiik) applied.

comment:5 by Tim Graham, 11 years ago

Component: Database layer (models, ORM)Migrations

Fix was committed in 630eb0564abd228da439d86ad93acb4089d795e7, but not the test.

comment:6 by Andrew Godwin <andrew@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 6f7977bb63ea592faaa7b3bdf2898f8361f30260:

Fixed #21029: Test for previously-commited SchemaEditor.exit bug.

Note: See TracTickets for help on using tickets.
Back to Top