Skip to content

Release session in close() even if rollback fails - #43

Merged
vgvoleg merged 2 commits into
mainfrom
fix/close-releases-session
Aug 27, 2026
Merged

Release session in close() even if rollback fails#43
vgvoleg merged 2 commits into
mainfrom
fix/close-releases-session

Conversation

@vgvoleg

@vgvoleg vgvoleg commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #42

close() called rollback() first and released the session only afterwards. If rollback() raised — the expected outcome when the session was invalidated mid-request — the release was skipped, the session was abandoned and its pool slot was lost permanently.

Session release, _tx_context reset and owned pool/driver shutdown now happen in a finally, in both Connection and AsyncConnection. The rollback error is still propagated.

Test: begin(), force tx_context.rollback to raise BadSession, assert close() raises but still releases the session back to the pool.

@vgvoleg
vgvoleg requested review from LuckySting and a lite review from Copilot August 6, 2026 11:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new regression test monkeypatches internal methods without restoring them, which can cause cascading fixture-teardown failures and make test failures flaky/harder to diagnose.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR fixes a resource-leak scenario in the YDB DB-API connection teardown path: if rollback() raises (e.g., due to an invalidated session), close() now still releases the checked-out session back to the pool and performs owned pool/driver shutdown, for both sync and async connections.

Changes:

  • Update Connection.close() and AsyncConnection.close() to perform session release, _tx_context reset, and owned pool/driver shutdown in a finally block even when rollback() raises.
  • Add a regression test that forces tx_context.rollback() to raise BadSession and asserts that close() still releases the session and clears connection state (sync + async variants).
File summaries
File Description
ydb_dbapi/connections.py Ensures close() always performs cleanup (release/reset/stop) via try/finally in both sync and async connections.
tests/test_connections.py Adds a regression test covering the “rollback raises but close still releases session” scenario for both sync and async connections.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread tests/test_connections.py
Comment on lines +79 to +95
released = []
original_release = connection._session_pool.release

def release(session: ydb.QuerySession) -> any:
released.append(session)
return original_release(session)

connection._session_pool.release = release

def failing_rollback(*args: any, **kwargs: any) -> None:
raise ydb.issues.BadSession("session is invalidated")

connection._tx_context.rollback = failing_rollback

with pytest.raises(dbapi.Error):
maybe_await(connection.close())

@vgvoleg
vgvoleg merged commit 0aa4600 into main Aug 27, 2026
6 checks passed
@vgvoleg
vgvoleg deleted the fix/close-releases-session branch August 27, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection.close() never releases the session back to the pool when rollback() raises

3 participants