Skip to content

Free Query Tool results when connections are released, and release the rest when one fails. #10890 - #10916

Merged
dpage merged 1 commit into
pgadmin-org:masterfrom
dpage:fix/issue-10890-release-closed-cursors
Dec 1, 2026
Merged

dpage merged 1 commit into
pgadmin-org:masterfrom
dpage:fix/issue-10890-release-closed-cursors

Conversation

@dpage

@dpage dpage commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Connection._release() now closes and drops the cursor of the last asynchronous query. Previously a released Connection that was still referenced kept every row that query had fetched, which is the multi-GB retention in the report.
  • ServerManager.release() logs and carries on when cancelling or closing one connection raises, so one bad connection no longer leaves the whole manager unreleased (and failing again on every idle-session sweep).
  • When the idle-session cleanup is triggered by another pgAdmin user's request, cancel_transaction() no longer decrypts the owner's saved password with the current user's key; it connects without it instead. The UnicodeDecodeError itself was already handled on master by fix: don't hard-fail on an undecryptable saved password #10174.

Test plan

  • New utils.driver.psycopg3.tests.test_release_connections (fails on master, passes here)
  • utils.driver.psycopg3.tests, tools.sqleditor.tests.test_poll_query_tool, test_connect_keeps_tool_connections, test_check_connect against PostgreSQL 18
  • pycodestyle on the changed files

Closes #10890

Summary by CodeRabbit

  • Bug Fixes
    • Improved connection cleanup so asynchronous cursors are closed even when a connection is already unavailable or cursor closing fails.
    • Improved server cleanup so failures while cancelling transactions or releasing connections do not prevent other connections from being processed.
    • Corrected saved-password handling during transaction cancellation, avoiding use of credentials when they do not belong to the current account.

…e rest when one fails. pgadmin-org#10890

Connection._release() closed the database connection but kept the cursor
of the last asynchronous query, and with it every row that query had
fetched, so a Connection object that outlived its release kept the whole
result set in memory. The cursor is now closed and dropped as well.

ServerManager.release() cancelled each connection's transaction and then
closed it with no error handling, so an exception from one connection
(such as a saved password that could not be decrypted) stopped the
remaining connections being closed and left all of them in the manager,
to fail again on every idle-session sweep. Failures are now logged and
the remaining connections are still released.

When another user's request runs the idle-session cleanup,
cancel_transaction() no longer tries to decrypt the saved password with
that user's key, and connects without it instead.

Closes pgadmin-org#10890
@dpage dpage self-assigned this Dec 1, 2026
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes update psycopg3 connection cleanup to close asynchronous cursors, handle per-connection failures during manager release, and avoid decrypting a saved password when the connection belongs to another pgAdmin user. Regression tests cover these cases.

Changes

Connection Cleanup

Layer / File(s) Summary
Asynchronous cursor release
web/pgadmin/utils/driver/psycopg3/connection.py, web/pgadmin/utils/driver/psycopg3/tests/test_release_connections.py
Connection._release() now detaches and closes its asynchronous cursor even when the connection is not marked connected. It logs cursor-close failures. Tests cover connected and disconnected connections, and a cursor-close error.
Cancellation and manager cleanup
web/pgadmin/utils/driver/psycopg3/connection.py, web/pgadmin/utils/driver/psycopg3/server_manager.py, web/pgadmin/utils/driver/psycopg3/tests/test_release_connections.py
cancel_transaction() skips saved-password decryption when the connection belongs to another pgAdmin user. ServerManager.release() logs cancellation and release errors and continues processing connections. Tests cover password handling and continued cleanup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • pgadmin-org/pgadmin4#10174: Handles undecryptable saved passwords in Connection._decode_password(); this PR addresses saved-password decryption during transaction cancellation.

Suggested reviewers: kundansable

Merge Risk: Minimal · up to 58e15

This change releases query result memory when connections close, keeps cleanup going if one connection fails, and avoids decrypting another user's saved password during cancellation. No actionable merge-blocking risk was found. The new tests cover the changed behavior with mocked connections.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #10890 has three coding requirements. (1) Release query results and cursor resources when connections close or sessions clean up. (2) A failure cancelling one transaction must not block cleanup … Make the connection.py diff and the test_release_connections.py content readable at the reviewed head. Confirm that Connection._release() closes and drops self.__async_cursor when the connection is not connected or close raises. Confirm tha…
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names two changes that the changeset implements: releasing Query Tool result cursors when connections are released (Connection._release clears and closes the async cursor), and continuing to…
Out of Scope Changes check Passed The change set, per the whole-PR summary, touches three areas: psycopg3/connection.py, psycopg3/server_manager.py, and a new regression test file. Each area maps to a #10890 requirement. The verified …
Full details: Title check

Explanation

The title names two changes that the changeset implements: releasing Query Tool result cursors when connections are released (Connection._release clears and closes the async cursor), and continuing to release the remaining connections when one fails (ServerManager.release logs and continues). The cancel_transaction password-decryption change is not in the title, but the title covers the main change and is not misleading. The title is long (16 words) and includes an issue number, which slightly reduces readability.

Full details: Out of Scope Changes check

Explanation

The change set, per the whole-PR summary, touches three areas: psycopg3/connection.py, psycopg3/server_manager.py, and a new regression test file. Each area maps to a #10890 requirement. The verified server_manager.py diff adds per-connection exception handling in release(), which is in scope for requirement (2). The connection.py changes address cursor release and the decryption key mismatch, which are in scope for requirements (1) and (3). The test file exercises these same behaviors. The PR description cites #10174 only as prior context and adds no unrelated code. No change was found that lacks a connection to the linked issue.

Full details: Linked Issues check

Explanation

Issue #10890 has three coding requirements. (1) Release query results and cursor resources when connections close or sessions clean up. (2) A failure cancelling one transaction must not block cleanup of other connections. (3) Cleanup must not decrypt another user's saved password with the current request user's key. Requirement (2) is verified at the reviewed head. In web/pgadmin/utils/driver/psycopg3/server_manager.py, ServerManager.release() wraps cancel_transaction() and _release() in separate try/except blocks that log and continue. The diff for web/pgadmin/utils/driver/psycopg3/connection.py could not be read because its changed blobs are missing from the checkout. Requirements (1) and (3) therefore rest only on the AI-generated summary. That summary states that Connection._release() clears and closes the async cursor, and that cancel_transaction() skips decryption when pga_user differs from the owner's fs_uniquifier. The summary is consistent with the verified server_manager.py change and with the PR test plan, but it is not source evidence for the connection.py behavior. The PR test file web/pgadmin/utils/driver/psycopg3/tests/test_release_connections.py was also not read. Requirements (1) and (3) cannot be confirmed from source.

Resolution

Make the connection.py diff and the test_release_connections.py content readable at the reviewed head. Confirm that Connection._release() closes and drops self.__async_cursor when the connection is not connected or close raises. Confirm that cancel_transaction() does not call decrypt() with the current user's crypt key for a saved password owned by another user.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4129152 and 58e1526.

Files selected for processing: 3

  • web/pgadmin/utils/driver/psycopg3/connection.py
  • web/pgadmin/utils/driver/psycopg3/server_manager.py
  • web/pgadmin/utils/driver/psycopg3/tests/test_release_connections.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

@dpage
dpage merged commit f5c3d74 into pgadmin-org:master Dec 1, 2026
46 of 47 checks passed
@dpage
dpage deleted the fix/issue-10890-release-closed-cursors branch December 1, 2026 12:01
draoncc added a commit to draoncc/pgadmin4 that referenced this pull request Dec 2, 2026
…g#9261

CancelOtherUsersTransactionTest, from pgadmin-org#10916, mocks a manager with no
auth_method, which cancel_transaction() takes for a token method and
cancels with libpq's cancel request. The test is about decrypting a saved
password, which only a password server does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Query Tool retains large result sets after connection closure, causing excessive memory usage

1 participant