]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
Reset Session._flushing when a bulk_* call can't begin its transaction
authorHamody We <iosapk.org@gmail.com>
Tue, 11 Aug 2026 14:10:30 +0000 (10:10 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 11 Aug 2026 14:15:15 +0000 (10:15 -0400)
commit4914050a66d0dcb7ec1e5df5a8810fb0c08f25ce
treeb3adb4be8e7cfe77b4a8b04cc27644d013e93521
parentc29adccc3ddfcb027e3c556fef9581406ab65115
Reset Session._flushing when a bulk_* call can't begin its transaction

Fixed bug where a failed ``Session.bulk_insert_mappings()``,
``Session.bulk_update_mappings()`` or ``Session.bulk_save_objects()`` call
could leave the :class:`_orm.Session` permanently in a "flushing" state.

``Session._bulk_save_mappings()`` set ``self._flushing = True`` and called
``self._autobegin_t()._begin()`` before its ``try`` block, so an exception
raised by ``_begin()`` -- such as ``PendingRollbackError`` when the
transaction still needs a rollback from a prior failed flush -- left
``_flushing`` stuck at ``True``.  As neither ``Session.rollback()`` nor
``Session.close()`` reset the flag, a reused Session then raised
"Session is already flushing" for every subsequent flush.

The flag-set and transaction-begin are moved inside a ``try``/``finally``
that wraps the whole method, matching the shape used by ``Session.flush()``,
so that the flag is always cleared regardless of where the failure occurs.

Fixes: #13485
Closes: #13487
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13487
Pull-request-sha: 4b3283906123068744943fa724722ee63b74e007

Change-Id: I5be8b59db500aacf2a76fe31f9c0c854a8324f70
doc/build/changelog/unreleased_20/13485.rst [new file with mode: 0644]
lib/sqlalchemy/orm/session.py
test/orm/test_session.py