Add session cleanup middleware to FAB FastAPI app#61480
Merged
shahar1 merged 2 commits intoapache:mainfrom Feb 10, 2026
Merged
Conversation
|
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)
|
91879a5 to
51e8bc2
Compare
2 tasks
2 tasks
Add HTTP middleware to get_fastapi_app() that calls Session.remove() after each request. This prevents PendingRollbackError that can occur when database connections time out while transactions are in 'idle in transaction' state. Unlike Flask's teardown_appcontext, FastAPI doesn't automatically clean up SQLAlchemy scoped sessions, which can leave transactions open and eventually cause session invalidation errors.
ed041a1 to
d89c8be
Compare
shahar1
approved these changes
Feb 10, 2026
|
Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions. |
81 tasks
Alok-kumar-priyadarshi
pushed a commit
to Alok-kumar-priyadarshi/airflow
that referenced
this pull request
Feb 11, 2026
Ratasa143
pushed a commit
to Ratasa143/airflow
that referenced
this pull request
Feb 15, 2026
Member
|
NIce one ! Thanks @yulit0738 ! |
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
PendingRollbackErroron FAB admin pages (/auth/users/list/,/auth/roles/list/) by adding session cleanup middleware to the FAB auth manager's FastAPI app.Problem
FAB auth manager's FastAPI app has this route structure:
/token,/logout→ FastAPI routes/users/*,/roles/*→ FastAPI API routes/*(catch-all) →WSGIMiddleware→ Flask App (FAB views)The Flask AppBuilder views (e.g.,
/users/list/,/roles/list/) usesettings.Session(SQLAlchemyscoped_session) for database access. In a native Flask app,teardown_appcontextautomatically callsSession.remove()after each request. However, when Flask is mounted viaWSGIMiddlewareinside FastAPI, Flask's teardown hooks do not trigger.This causes sessions to remain in "idle in transaction" state. When the database connection times out (e.g., PostgreSQL's
idle_in_transaction_session_timeout), subsequent requests that reuse the invalidated session raisePendingRollbackError(500 error).Steps to reproduce
/auth/users/list/or/auth/roles/list/Solution
Add a FastAPI HTTP middleware in
get_fastapi_app()that callsSession.remove()in thefinallyblock after every request. This ensures session cleanup for all requests, including those forwarded to Flask via WSGI — closing the gap left by the missingteardown_appcontext.Why this is safe
settings.Sessionis ascoped_session(thread-local) —remove()only affects the current threadfinallyblock runs after the response is fully constructed, so it doesn't interfere with request processingTesting
TestFabAuthManagerSessionCleanupwith 2 tests:test_session_cleanup_middleware_on_wsgi_route: VerifiesSession.remove()is called after WSGI (Flask) route requests — the exact scenario that caused the bugtest_session_cleanup_middleware_on_fastapi_route: Verifies cleanup also runs after FastAPI route requestsAI Disclosure
This PR was developed with AI assistance (Claude).