Repository navigation
Reset _flushing when a bulk_* call fails before it can begin - #13488
Closed
alliasgher wants to merge 1 commit into
Closed
alliasgher wants to merge 1 commit into
alliasgher wants to merge 1 commit into
Conversation
_bulk_save_mappings set self._flushing = True and called self._autobegin_t()._begin() above its try:, so the finally: that resets the flag never ran if _begin() raised. On a Session whose transaction is not active, _begin() raises PendingRollbackError through its @_StateChange.declare_states guard. The Session was then stuck: _flushing is only ever cleared in __init__, flush() and here, so neither rollback() nor close() recovers it and every later flush() raises 'Session is already flushing'. The assignment stays above _begin(): SessionTransaction.__init__ autoflushes when the flag is not set, so moving it below would recurse. Wrap _begin() and the existing try/except in the try instead. Fixes: sqlalchemy#13485
Member
|
hi, thanks but there is already #13487 open |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13485
Description
Session._bulk_save_mappingssets the flushing flag and begins the transaction above itstry::_begin()carries@_StateChange.declare_states((SessionTransactionState.ACTIVE,), NO_CHANGE), so on a Session whose transaction is not active it raisesPendingRollbackError— before thetry:is entered. Thefinally:never runs and_flushingstaysTrue.The Session is then unrecoverable:
grep -n "_flushing"shows the flag is only ever set back toFalsein__init__, inflush(), and in thatfinally:. Neitherrollback()norclose()touches it, so the recovery thePendingRollbackErrormessage advises does not work, and every subsequentflush()raisesInvalidRequestError: Session is already flushing.flush()itself is fine — it sets the flag inside itstry:(session.py:4504-4513), which is why the reporter's step 1 self-heals and only the bulk path sticks.Reproduced on current main (2.1.0b4, 5995834) with the reporter's script; output matches their 1.4.54 / 2.0.51 results exactly.
Fix
The obvious-looking fix — move
self._flushing = Truebelow_begin()and into thetry:— would be wrong, and I want to flag that explicitly since it is the first thing a reviewer will reach for._begin()constructs aSessionTransactionwith originSUBTRANSACTION, andSessionTransaction.__init__does:So the flag being set before
_begin()is load-bearing — it suppresses a recursive autoflush on the bulk path. I kept the assignment where it is and wrapped_begin()plus the existingtry/exceptin thetry:instead, which is a pure indentation change to the existing body.Tests
Added to
SessionTransactionTestintest/orm/test_transaction.pyrather thantest/orm/dml/test_bulk.py, because that class already has_inactive_flushed_session_fixture(), which produces exactly the deactivated-transaction state this needs, and itsFixtureTestbase handles table lifecycle for a session that is deliberately left mid-failure. (I tried it intest_bulk.pyfirst;BulkTest.run_define_tables = "each"plussqlite:///:memory:meant a deliberately un-rolled-back session took the schema with it between tests.)The test parameterises over
bulk_update_mappingsandbulk_insert_mappings, asserts_flushingisFalseafter thePendingRollbackError, and then asserts the documented recovery works —rollback(), add,commit()— since silently losing the Session is the actual user-visible symptom.Verified non-vacuous: with
lib/sqlalchemy/orm/session.pyreverted both parameterisations fail; with the fix both pass.test/orm/in full: 10880 passed, 142 skipped.black --checkclean on both changed files;flake8reports the same 21 pre-existingI100import-order notes onsession.pybefore and after.Changelog
doc/build/changelog/unreleased_20/13485.rst, since the issue is milestoned2.0.x.