Repository navigation
Release the scoped session on the handler thread in the FAB FastAPI routes - #76403
Merged
Merged
Conversation
jecos
force-pushed
the
fix/fab-single-item-session-leak
branch
from
November 3, 2026 22:39
f241196 to
a3a07de
Compare
jecos
marked this pull request as ready for review
November 3, 2026 22:39
vincbeck
approved these changes
Nov 16, 2026
|
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.
The sync FAB FastAPI routes leave the thread-local
settings.Sessionopen on the threadpool thread that ran them. A read such asfind_userorfind_roleopens a transaction that nothing ends, so on PostgreSQL the backend staysidle in transactionand the connection is never returned to the pool.cleanup_session_middlewarecallsSession.remove(), but it runs on the event-loop thread and never reaches the worker thread's session. A client that pollsGET /auth/fab/v1/users/{username}or/roles/{name}exhausts the API server's pool (QueuePool limit of size 5 overflow 10 reached): health checks time out, the liveness probe restarts the pod, and tasks lose the Execution API.#72578 and #72950 fixed the collection routes by moving them to
create_session(scoped=False). The single-object and write routes still go through the security manager (find_user,find_role,add_user,update_user,bulk_sync_roles, ...), which is bound to the scoped session. Converting each of those paths would mean rewriting the security manager calls one by one. This PR instead releases the session at the request boundary, on the handler thread, for every route.Changes
routes/login.py: new_release_scoped_session()context manager. On exit, it callssettings.Session.remove()on the current thread, and logs instead of raising if the remove fails, so it never masks the response.routes/users.py,routes/roles.py,routes/login.py: every sync route enters it next to_get_flask_app().app_context(), so the session is removed after the response model has been built.tests/.../routes/test_session_release.py:remove(), noSessionconfigured;GET/DELETEon/users/{username}and/roles/{name}remove the session on the handler thread, not only on the middleware's thread. These four route tests fail without the change.I did not register a
teardown_appcontexton the FAB Flask app, which would be the Flask-native place for this. It would also run on the nestedapp_context()blocks inwww/app.pyandmodels/db.pyand could detach objects still in use.Testing
providers/fab:uv run pytest tests/unit/fab/auth_manager/api_fastapi: 165 passed. One error intest_parameters.pycomes from my local environment (ModuleNotFoundError: airflow_shared) and is unrelated.mainwithout the route change (4 failed, 4 passed) and pass with it.ruff formatandruff checkon the changed files.closes: #76402
related: #72362
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Claude Opus 5.5) following the guidelines