-
Notifications
You must be signed in to change notification settings - Fork 895
Fix BEGIN/COMMIT/ROLLBACK silently failing under server cursor mode #10321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dpage
wants to merge
6
commits into
pgadmin-org:master
Choose a base branch
from
dpage:fix/8991-servercursor-commit-rollback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+447
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95111cc
fix: run BEGIN/COMMIT/ROLLBACK on a plain cursor under server cursor …
dpage 8072ca2
fix: guard explain_query_length against an async cursor with no query…
dpage 5f84ba8
fix: point the async cursor at the throwaway transaction-control cursor
dpage 9019962
Only detach the async cursor for transaction-control statements
dpage a94372c
Skip leading SQL comments when classifying transaction control
dpage 9556df0
Read the transaction-control keyword as a whole word
dpage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
90 changes: 90 additions & 0 deletions
90
web/pgadmin/tools/sqleditor/tests/test_poll_explain_query_length_guard.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| """Regression test for a review comment on PR #10321 (pgAdmin issue | ||
| #8991): poll()'s error-handling branch built the 'explain_query_length' | ||
| value with:: | ||
|
|
||
| get_explain_query_length(conn._Connection__async_cursor._query) | ||
| if conn._Connection__async_cursor else 0 | ||
|
|
||
| which only guarded against the cached async cursor itself being falsy, | ||
| not against its ``_query`` attribute being ``None``. PR #10321's own fix | ||
| runs BEGIN/COMMIT/ROLLBACK through a throwaway plain cursor under | ||
| "server cursor" mode; once that has happened the cached async cursor | ||
| that poll() sees next can be a cursor that has not yet executed a real | ||
| statement, so ``_query`` is still ``None``. get_explain_query_length() | ||
| then immediately does ``query_obj.query.decode()``, and with | ||
| ``query_obj`` being ``None`` that crashes with:: | ||
|
|
||
| AttributeError: 'NoneType' object has no attribute 'query' | ||
|
|
||
| turning any query error that follows a commit under "server cursor" | ||
| mode into an unhandled 500 and leaving the Query Tool unusable, instead | ||
| of the normal JSON error response.""" | ||
|
|
||
| import json | ||
| import secrets | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from pgadmin.utils.route import BaseTestGenerator | ||
|
|
||
|
|
||
| class TestPollExplainQueryLengthGuard(BaseTestGenerator): | ||
| """poll() must not crash while building 'explain_query_length' when | ||
| the cached async cursor has not yet executed any statement.""" | ||
|
|
||
| scenarios = [ | ||
| ('Cached async cursor has not executed a statement yet ' | ||
| '(_query is None) - poll() must not crash', dict()) | ||
| ] | ||
|
|
||
| def runTest(self): | ||
| trans_id = secrets.choice(range(1, 9999999)) | ||
|
|
||
| # A cursor left over from execute_void()'s throwaway plain | ||
| # cursor (or a freshly (re)created server-side cursor) that has | ||
| # not executed a real statement yet - exactly the state PR | ||
| # #10321's own fix can leave behind after a commit under | ||
| # "server cursor" mode. | ||
| async_cursor = MagicMock() | ||
| async_cursor._query = None | ||
|
|
||
| conn = MagicMock() | ||
| conn.poll.return_value = (False, 'some query error') | ||
| conn.connected.return_value = True | ||
| conn.messages.return_value = [] | ||
| conn.transaction_status.return_value = 0 | ||
| conn._Connection__async_cursor = async_cursor | ||
|
|
||
| trans_obj = MagicMock() | ||
| trans_obj.get_thread_native_id.return_value = None | ||
|
|
||
| session_obj = {} | ||
|
|
||
| with patch( | ||
| 'pgadmin.tools.sqleditor.check_transaction_status', | ||
| return_value=(True, None, conn, trans_obj, session_obj) | ||
| ): | ||
| response = self.tester.get( | ||
| '/sqleditor/poll/{0}'.format(trans_id)) | ||
|
|
||
| # Before the fix this either raised AttributeError outright, or | ||
| # (via the app's generic exception handler) came back as a 500 | ||
| # whose errormsg was the raw AttributeError text instead of the | ||
| # intended query-error response. | ||
| response_text = response.data.decode('utf-8') | ||
| self.assertNotIn( | ||
| "'NoneType' object has no attribute 'query'", response_text) | ||
|
|
||
| response_data = json.loads(response_text) | ||
| self.assertEqual(response.status_code, 500) | ||
| self.assertEqual(response_data['errormsg'], 'some query error') | ||
| self.assertEqual( | ||
| response_data['data']['explain_query_length'], 0) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.