providers-fab: Handle database errors in cleanup_session_middleware Session.remove()#62336
Merged
vincbeck merged 7 commits intoapache:mainfrom Feb 24, 2026
Conversation
When Session.remove() is called in the finally block of cleanup_session_middleware, it may raise an OperationalError if the underlying database connection is already dead (e.g., MySQL 'Server has gone away', Aurora failover, network timeout). The unhandled exception propagates as a 500 Internal Server Error to the client, even when the original request succeeded. This commit wraps Session.remove() in a try-except block that catches and logs the error as a warning, consistent with how session cleanup is handled elsewhere in Airflow. Fixes follow-up to apache#61480.
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
|
2 tasks
vincbeck
approved these changes
Feb 23, 2026
Contributor
|
Static checks are failing, running |
Define module logger for cleanup warnings and drop unused test response assignment to satisfy ruff.
Move logger definition below imports to satisfy static checks and assert warning logging in session cleanup tests so failures are surfaced clearly.
|
Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix unhandled
MySQLdb.OperationalError: (2006, 'Server has gone away')incleanup_session_middlewarewhenSession.remove()encounters a dead database connection.Closes: #62335
Problem
PR #61480 introduced
cleanup_session_middlewareinproviders-fab 3.3.0to fixPendingRollbackError(#59349). The middleware callsSession.remove()in a barefinallyblock:When the underlying database connection has been closed server-side (MySQL timeout, Aurora failover, network interruption),
Session.remove()internally attempts aROLLBACKon the dead connection, raisingOperationalError. This unhandled exception propagates as a 500 Internal Server Error — even though the original request completed successfully.Production error log:
Solution
Wrap
Session.remove()in a try-except that catches and logs the error as a warning:This is consistent with session cleanup patterns elsewhere in Airflow (e.g.,
airflow/utils/session.py).Why this is safe
Session.remove()is a cleanup operation — if it fails because the connection is already dead, the session will be discarded anyway on the next requestexcept Exceptionis intentionally broad since any error during cleanup should not affect the HTTP responseTesting
Added
TestFabAuthManagerSessionCleanupErrorHandlingwith 2 tests:test_session_remove_db_error_does_not_propagate: VerifiesOperationalError(MySQL 'Server has gone away') is caughttest_session_remove_generic_error_does_not_propagate: Verifies other exceptions (e.g.,RuntimeError) are also caughtRelated
PendingRollbackErrorissuecleanup_session_middleware(this PR fixes a gap in that implementation)AI Disclosure
This PR was developed with AI assistance.