This is an automated email from the ASF dual-hosted git repository.

vincbeck pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new e766e71cae7 Release the scoped session on the handler thread in the 
FAB FastAPI routes (#76403)
e766e71cae7 is described below

commit e766e71cae70ab83d0df2e373adc2b5bae68e961
Author: Jeremy Costanza <[email protected]>
AuthorDate: Mon Nov 16 14:52:49 2026 -0500

    Release the scoped session on the handler thread in the FAB FastAPI routes 
(#76403)
---
 .../fab/auth_manager/api_fastapi/routes/login.py   |  35 +++++-
 .../fab/auth_manager/api_fastapi/routes/roles.py   |  17 +--
 .../fab/auth_manager/api_fastapi/routes/users.py   |  15 +--
 .../api_fastapi/routes/test_session_release.py     | 117 +++++++++++++++++++++
 4 files changed, 168 insertions(+), 16 deletions(-)

diff --git 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/login.py
 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/login.py
index 7d5b9750b7b..88fc531da6f 100644
--- 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/login.py
+++ 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/login.py
@@ -17,6 +17,8 @@
 from __future__ import annotations
 
 import logging
+from collections.abc import Generator
+from contextlib import contextmanager
 from typing import Any
 
 from fastapi import Body, HTTPException, Request, status
@@ -67,6 +69,33 @@ def _get_flask_app():
     return auth_manager.flask_app
 
 
+@contextmanager
+def _release_scoped_session() -> Generator[None, None, None]:
+    """
+    Remove the thread-local ``settings.Session`` when a sync FAB route returns.
+
+    FastAPI runs sync route handlers on a threadpool thread. The FAB security 
manager reads and
+    writes through ``settings.Session``, a ``scoped_session`` keyed by thread, 
so the session it
+    opens stays bound to that worker thread. ``cleanup_session_middleware`` 
calls
+    ``Session.remove()`` on the event-loop thread and never reaches it: the 
transaction opened by
+    a read such as ``find_user`` or ``find_role`` is never ended, the backend 
stays
+    ``idle in transaction`` and the connection is never returned to the pool. 
Once every worker
+    thread holds one, the pool is exhausted and the API server stops answering.
+
+    Removing the session here runs in the handler's own thread, after the 
response model is built.
+    """
+    try:
+        yield
+    finally:
+        from airflow import settings
+
+        if settings.Session is not None:
+            try:
+                settings.Session.remove()
+            except Exception:
+                log.warning("Failed to remove the scoped session after a FAB 
API call", exc_info=True)
+
+
 @auth_router.post(
     "/token",
     response_model=LoginResponse,
@@ -75,7 +104,7 @@ def _get_flask_app():
 )
 def create_token(request: Request, body: dict[str, Any] = Body(...)) -> 
LoginResponse:
     """Generate a new API token."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerLogin.create_token(headers=dict(request.headers), 
body=body)
 
 
@@ -87,7 +116,7 @@ def create_token(request: Request, body: dict[str, Any] = 
Body(...)) -> LoginRes
 )
 def create_token_cli(request: Request, body: dict[str, Any] = Body(...)) -> 
LoginResponse:
     """Generate a new CLI API token."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerLogin.create_token(
             headers=dict(request.headers),
             body=body,
@@ -101,7 +130,7 @@ def create_token_cli(request: Request, body: dict[str, Any] 
= Body(...)) -> Logi
 )
 def logout(request: Request) -> RedirectResponse:
     """Clear session cookies and redirect to the login page."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         login_url = get_auth_manager().get_url_login()
         secure = request.base_url.scheme == "https" or bool(conf.get("api", 
"ssl_cert", fallback=""))
         cookie_path = get_cookie_path()
diff --git 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
index 654f6f97a4e..b467b6a9515 100644
--- 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
+++ 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
@@ -26,7 +26,10 @@ from 
airflow.providers.fab.auth_manager.api_fastapi.datamodels.roles import (
     RoleResponse,
 )
 from airflow.providers.fab.auth_manager.api_fastapi.parameters import 
get_effective_limit
-from airflow.providers.fab.auth_manager.api_fastapi.routes.login import 
_get_flask_app
+from airflow.providers.fab.auth_manager.api_fastapi.routes.login import (
+    _get_flask_app,
+    _release_scoped_session,
+)
 from airflow.providers.fab.auth_manager.api_fastapi.routes.router import 
fab_router
 from airflow.providers.fab.auth_manager.api_fastapi.security import 
requires_fab_custom_view
 from airflow.providers.fab.auth_manager.api_fastapi.services.roles import 
FABAuthManagerRoles
@@ -48,7 +51,7 @@ from airflow.providers.fab.www.security import permissions
 )
 def create_role(body: RoleBody) -> RoleResponse:
     """Create a new role (actions can be empty)."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.create_role(body=body)
 
 
@@ -71,7 +74,7 @@ def get_roles(
     offset: int = Query(0, ge=0, description="Number of items to skip before 
starting to collect results."),
 ) -> RoleCollectionResponse:
     """List roles with pagination and ordering."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.get_roles(order_by=order_by, limit=limit, 
offset=offset)
 
 
@@ -88,7 +91,7 @@ def get_roles(
 )
 def delete_role(name: str = Path(..., min_length=1)) -> None:
     """Delete an existing role."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.delete_role(name=name)
 
 
@@ -101,7 +104,7 @@ def delete_role(name: str = Path(..., min_length=1)) -> 
None:
 )
 def get_role(name: str = Path(..., min_length=1)) -> RoleResponse:
     """Get an existing role."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.get_role(name=name)
 
 
@@ -123,7 +126,7 @@ def patch_role(
     update_mask: str | None = Query(None, description="Comma-separated list of 
fields to update"),
 ) -> RoleResponse:
     """Update an existing role."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.patch_role(name=name, body=body, 
update_mask=update_mask)
 
 
@@ -146,5 +149,5 @@ def get_permissions(
     offset: int = Query(0, ge=0, description="Number of items to skip before 
starting to collect results."),
 ) -> PermissionCollectionResponse:
     """List all action-resource (permission) pairs."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerRoles.get_permissions(order_by=order_by, 
limit=limit, offset=offset)
diff --git 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/users.py
 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/users.py
index 3d6312704e5..29d0f8820b5 100644
--- 
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/users.py
+++ 
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/users.py
@@ -26,7 +26,10 @@ from 
airflow.providers.fab.auth_manager.api_fastapi.datamodels.users import (
     UserResponse,
 )
 from airflow.providers.fab.auth_manager.api_fastapi.parameters import 
get_effective_limit
-from airflow.providers.fab.auth_manager.api_fastapi.routes.login import 
_get_flask_app
+from airflow.providers.fab.auth_manager.api_fastapi.routes.login import (
+    _get_flask_app,
+    _release_scoped_session,
+)
 from airflow.providers.fab.auth_manager.api_fastapi.routes.router import 
fab_router
 from airflow.providers.fab.auth_manager.api_fastapi.security import 
requires_fab_custom_view
 from airflow.providers.fab.auth_manager.api_fastapi.services.users import 
FABAuthManagerUsers
@@ -47,7 +50,7 @@ from airflow.providers.fab.www.security import permissions
     dependencies=[Depends(requires_fab_custom_view("POST", 
permissions.RESOURCE_USER))],
 )
 def create_user(body: UserBody) -> UserResponse:
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerUsers.create_user(body=body)
 
 
@@ -69,7 +72,7 @@ def get_users(
     offset: int = Query(0, ge=0, description="Number of items to skip before 
starting to collect results."),
 ) -> UserCollectionResponse:
     """List users with pagination and ordering."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerUsers.get_users(order_by=order_by, limit=limit, 
offset=offset)
 
 
@@ -86,7 +89,7 @@ def get_users(
 )
 def get_user(username: str = Path(..., min_length=1)) -> UserResponse:
     """Get a user by username."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerUsers.get_user(username=username)
 
 
@@ -109,7 +112,7 @@ def update_user(
     update_mask: str | None = Query(None, description="Comma-separated list of 
fields to update"),
 ) -> UserResponse:
     """Update an existing user."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         return FABAuthManagerUsers.update_user(username=username, body=body, 
update_mask=update_mask)
 
 
@@ -127,5 +130,5 @@ def update_user(
 )
 def delete_user(username: str = Path(..., min_length=1)):
     """Delete a user by username."""
-    with _get_flask_app().app_context():
+    with _get_flask_app().app_context(), _release_scoped_session():
         FABAuthManagerUsers.delete_user(username=username)
diff --git 
a/providers/fab/tests/unit/fab/auth_manager/api_fastapi/routes/test_session_release.py
 
b/providers/fab/tests/unit/fab/auth_manager/api_fastapi/routes/test_session_release.py
new file mode 100644
index 00000000000..19c9847ab29
--- /dev/null
+++ 
b/providers/fab/tests/unit/fab/auth_manager/api_fastapi/routes/test_session_release.py
@@ -0,0 +1,117 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import threading
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from airflow.providers.fab.auth_manager.api_fastapi.datamodels.roles import 
RoleResponse
+from airflow.providers.fab.auth_manager.api_fastapi.datamodels.users import 
UserResponse
+from airflow.providers.fab.auth_manager.api_fastapi.routes.login import 
_release_scoped_session
+
+ROUTES = "airflow.providers.fab.auth_manager.api_fastapi.routes"
+
+USER = UserResponse(
+    username="alice",
+    email="[email protected]",
+    first_name="Alice",
+    last_name="Liddell",
+    roles=None,
+    active=True,
+    login_count=0,
+    fail_login_count=0,
+)
+ROLE = RoleResponse(name="Viewer", permissions=[])
+
+
+class TestReleaseScopedSession:
+    @patch("airflow.settings.Session")
+    def test_removes_session_on_success(self, mock_session):
+        with _release_scoped_session():
+            pass
+        mock_session.remove.assert_called_once_with()
+
+    @patch("airflow.settings.Session")
+    def test_removes_session_when_the_body_raises(self, mock_session):
+        with pytest.raises(ValueError, match="boom"):
+            with _release_scoped_session():
+                raise ValueError("boom")
+        mock_session.remove.assert_called_once_with()
+
+    @patch("airflow.settings.Session")
+    def test_a_failing_remove_does_not_mask_the_result(self, mock_session):
+        mock_session.remove.side_effect = RuntimeError("connection gone")
+        with _release_scoped_session():
+            result = "ok"
+        assert result == "ok"
+
+    @patch("airflow.settings.Session", None)
+    def test_no_scoped_session_configured(self):
+        with _release_scoped_session():
+            pass
+
+
[email protected]_test
+class TestRoutesReleaseSessionOnHandlerThread:
+    """
+    The scoped session is thread-local, so it must be removed on the thread 
that ran the handler.
+
+    The ``Session.remove()`` issued by ``cleanup_session_middleware`` runs on 
the event-loop thread
+    and does not count: the assertion is on the handler thread.
+    """
+
+    @pytest.mark.parametrize(
+        ("method", "path", "module", "service", "service_method", "result"),
+        [
+            ("get", "/fab/v1/users/alice", "users", "FABAuthManagerUsers", 
"get_user", USER),
+            ("delete", "/fab/v1/users/alice", "users", "FABAuthManagerUsers", 
"delete_user", None),
+            ("get", "/fab/v1/roles/Viewer", "roles", "FABAuthManagerRoles", 
"get_role", ROLE),
+            ("delete", "/fab/v1/roles/Viewer", "roles", "FABAuthManagerRoles", 
"delete_role", None),
+        ],
+    )
+    def test_session_removed_on_handler_thread(
+        self, method, path, module, service, service_method, result, 
test_client, as_user
+    ):
+        handler_threads: list[int] = []
+        remove_threads: list[int] = []
+
+        def _service_call(*args, **kwargs):
+            handler_threads.append(threading.get_ident())
+            return result
+
+        mock_session = MagicMock()
+        mock_session.remove.side_effect = lambda: 
remove_threads.append(threading.get_ident())
+        auth_manager = MagicMock()
+        auth_manager.is_authorized_custom_view.return_value = True
+
+        with (
+            patch(f"{ROUTES}.{module}.{service}") as mock_service,
+            patch(f"{ROUTES}.{module}._get_flask_app"),
+            
patch("airflow.providers.fab.auth_manager.api_fastapi.security.get_auth_manager")
 as mock_gam,
+            patch("airflow.settings.Session", mock_session),
+            as_user(),
+        ):
+            mock_gam.return_value = auth_manager
+            getattr(mock_service, service_method).side_effect = _service_call
+            resp = getattr(test_client, method)(path)
+
+        assert resp.status_code < 300, resp.text
+        assert len(handler_threads) == 1
+        assert handler_threads[0] != threading.get_ident()
+        assert handler_threads[0] in remove_threads

Reply via email to