Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #46363 +/- ##
==========================================
+ Coverage 85.97% 85.98% +0.01%
==========================================
Files 3127 3128 +1
Lines 217726 217859 +133
Branches 51618 51644 +26
==========================================
+ Hits 187183 187325 +142
+ Misses 27119 27111 -8
+ Partials 3424 3423 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
aminghadersohi
left a comment
There was a problem hiding this comment.
The new rollback breaks SQLExecutor's failed-flush error handling; rolling back in the caller that has no rollback owner keeps both paths correct.
aminghadersohi
left a comment
There was a problem hiding this comment.
The moved rollback alters the executor's prepare-path error text and misses the flush site chart data reaches.
Nit: The description still describes the earlier oauth2.py rollback; the diff now changes Database._open_with_oauth2_retry.
aminghadersohi
left a comment
There was a problem hiding this comment.
The chart-data flush still fails outside the new guards, so the PendingRollbackError this PR targets remains on that path.
aminghadersohi
left a comment
There was a problem hiding this comment.
The staged token flush still escapes the new guards on real chart-data orders, so the session can stay inactive.
…th2_retry When refreshing a database's per-user OAuth2 token (e.g. during chart data query execution) a DB write to the token fails to flush -- for example, a statement timeout while persisting a rotated access/refresh token -- the SQLAlchemy session is left deactivated (is_active=False). The existing `execute_with_oauth2_retry` except-block detected this case and re-raised the original exception, but deliberately skipped calling session.rollback(), assuming some other "execution owner" further up the stack would do it. Nothing further up actually does this, so the broken session persists for the rest of the request. Any later ORM access in the same request (e.g. loading an expired attribute) then raises PendingRollbackError instead of a sane error, surfacing as an unhandled 500 far away from the real cause. Roll back the session immediately when we detect it has been deactivated, before re-raising the original exception. This keeps the original error visible (still handled by the normal exception/error-response machinery) while leaving the session usable for whatever runs next in the request. Updated the existing unit test (test_execute_with_oauth2_retry_preserves_failed_flush_error) which had asserted rollback was *not* called -- that assertion encoded the bug. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…in exception handler The rollback inside the except block is an exception-path cleanup pattern, not a general unit-of-work transaction, so the @transaction decorator does not apply. Suppress the warning inline to match existing codebase precedent (e.g. jinja_context.py:307, views/base.py:574, datasets/api.py:718). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Revert the rollback in execute_with_oauth2_retry, which reactivated the session before SQLExecutor checks db.session.is_active, and roll back in Database._open_with_oauth2_retry, the caller with no rollback owner. Add a real-session test that fails a flush while opening the connection. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…llback, scope) Limit the connection-open rollback to get_raw_connection so get_inspector keeps the executor's prepare-path error text, roll back the failed flush at get_df's own engine lookup, guard the rollback so it cannot mask the flush error, and restore ruff 0.9.7 layout on two unrelated test asserts. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…'s first ORM read Chart data compiles its query right after get_sqla_query's engine lookup, so the refresh staged there is flushed inside compile_sqla_query, before the get_df guard. In get_df, the first ORM read (db_engine_spec) preceded the guarded block, and check_for_oauth2 read the deactivated database and raised PendingRollbackError over the flush error. Share one rollback context manager across both sites, widen get_df's guard to cover its first ORM read, and have check_for_oauth2 re-raise on a deactivated session. Tests now fail a real flush on the session fixture. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The staged token refresh can be flushed by any ORM read while a chart query is built: the engine lookup, or the lazy load of unloaded columns and metrics. Guard get_sqla_query as a whole, resolving the rollback without touching the self.database relationship (itself a possible lazy load). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
8f1b07e to
fad86f0
Compare
aminghadersohi
left a comment
There was a problem hiding this comment.
The new get_sqla_query rollback changes the executor's sanitized prepare-path error text.
| col = self.make_sqla_column_compatible(col, label) | ||
| return col | ||
|
|
||
| @_rolling_back_failed_flush |
There was a problem hiding this comment.
Measured: when SQLExecutor renders a dataset() macro, a failed flush at a lazy load in get_sqla_query is now rolled back here first, so executor.py:718 returns the statement and its parameters instead of base's driver-only message; the compile_sqla_query and get_df guards do the same.
…okup The engine-lookup test's staged flush landed at the self.dttm_cols lazy load, duplicating the lazy-load test. Load columns and metrics before staging so the flush lands at the database engine lookup. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Summary
When a database's per-user OAuth2 access/refresh token is refreshed during query execution (e.g. a chart data request against a BigQuery/Snowflake/GSheets connection), the write to the token row is done on the request-scoped SQLAlchemy session. If that write fails to flush -- for example a statement timeout while updating the token row -- the session becomes deactivated (
session.is_activeisFalse) until an explicitrollback()is issued.execute_with_oauth2_retry(insuperset/utils/oauth2.py) already detects this case:but it deliberately skips calling
db.session.rollback(), assuming some caller further up the stack ("the execution owner") will do it. In practice nothing does. The original exception propagates, but the broken session persists for the rest of the request. Any later ORM access in the same request (e.g. loading an expired attribute while building the response) then raisesPendingRollbackErrorinstead of the real error, surfacing to the client as an unhandled 500 far away from the actual root cause (a timed-out write to the OAuth2 token table).Fix
Roll back the session immediately once we detect it has been deactivated by a failed flush, before re-raising the original exception:
This keeps the original exception visible (still handled by the normal error-response machinery, e.g. as an
OperationalError-> 500 for a genuine DB timeout) while leaving the session in a usable state for whatever runs next in the request, instead of letting it degrade into an opaquePendingRollbackError.Also updated the existing unit test
test_execute_with_oauth2_retry_preserves_failed_flush_error, which asserteddb.session.rollback.assert_not_called()-- that assertion encoded the behavior being fixed here. It now asserts rollback is called exactly once, while still confirming the original exception is preserved and propagated unchanged.Test plan
tests/unit_tests/utils/oauth2_tests.py(47 tests) passes, including the updated regression testtests/unit_tests/commands/databases/oauth2_test.py,tests/unit_tests/databases/oauth2_tokens_test.py,tests/unit_tests/databases/oauth2_api_test.py(89 tests) pass unaffected🤖 Generated with Claude Code