Repository navigation
Is a Session expected to be recoverable after a bulk_* call fails on an inactive transaction? #13484
Replies: 2 comments 2 replies
|
Reproduced this on 2.0.51 and 1.4.54, and it's still there on current main. It's a bug, not intended recovery behavior. In rollback() and close() leave |
|
Thanks for the report. It does indeed seems like a bug. I've created #13485 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
We hit a situation where a Session becomes permanently unusable, and I would like to understand whether this is expected behaviour and what the supported recovery is — before assuming it is a bug.
If a bulk_update_mappings() / bulk_insert_mappings() / bulk_save_objects() call is made while the Session's transaction is not active (for example after a failed flush that has not yet been rolled back), the call raises PendingRollbackError as I would expect — but the Session is left with _flushing set to True, and every subsequent flush() then raises InvalidRequestError: Session is already flushing.
What surprised us is that the usual recovery does not clear it: after Session.rollback() — which is what the PendingRollbackError message advises — and even after Session.close(), the Session still raises on the next flush(). In our case the Session is reused across units of work, so it stays broken until the object itself is discarded.
Reproduction
Output, identical on 1.4.54 and 2.0.51:
_flushing after failed flush: False
_flushing after bulk call: True
_flushing after close(): True
Note that step 1 shows flush() itself cleaning up correctly — it is only the bulk path where the flag persists.
Questions
Happy to open an issue if this turns out to be unintended rather than a usage mistake on our side.
Versions
OS: Linux
Python: 3.12.9
SQLAlchemy: reproduced on 1.4.54 and 2.0.51
Database: SQLite for the script above; originally observed on MySQL 8 / PyMySQL, where a deadlock during a bulk update is what leaves the transaction needing a rollback
DBAPI: sqlite3, pymysql
All reactions