This is an automated email from the ASF dual-hosted git repository.
eladkal pushed a commit to branch v3-1-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v3-1-test by this push:
new f6e85dd7696 Remove global from FastAPI app.py (#59772) (#62997)
f6e85dd7696 is described below
commit f6e85dd7696359d24a0f98a93492dfab00e67471
Author: Vincent <[email protected]>
AuthorDate: Sun Mar 8 04:27:31 2026 -0400
Remove global from FastAPI app.py (#59772) (#62997)
* Remove global from FastAPI app.py
* Remove global from FastAPI app.py
Co-authored-by: Jens Scheffler <[email protected]>
---
airflow-core/src/airflow/api_fastapi/app.py | 26 ++++++++++++--------------
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/airflow-core/src/airflow/api_fastapi/app.py
b/airflow-core/src/airflow/api_fastapi/app.py
index 1720c9f08ec..3a61c7895c7 100644
--- a/airflow-core/src/airflow/api_fastapi/app.py
+++ b/airflow-core/src/airflow/api_fastapi/app.py
@@ -18,6 +18,7 @@ from __future__ import annotations
import logging
from contextlib import AsyncExitStack, asynccontextmanager
+from functools import cache
from typing import TYPE_CHECKING, cast
from urllib.parse import urlsplit
@@ -64,8 +65,9 @@ RESERVED_URL_PREFIXES = ["/api/v2", "/ui", "/execution"]
log = logging.getLogger(__name__)
-app: FastAPI | None = None
-auth_manager: BaseAuthManager | None = None
+
+class _AuthManagerState:
+ instance: BaseAuthManager | None = None
@asynccontextmanager
@@ -117,19 +119,16 @@ def create_app(apps: str = "all") -> FastAPI:
return app
+@cache
def cached_app(config=None, testing=False, apps="all") -> FastAPI:
"""Return cached instance of Airflow API app."""
- global app
- if not app:
- app = create_app(apps=apps)
- return app
+ return create_app(apps=apps)
def purge_cached_app() -> None:
"""Remove the cached version of the app and auth_manager in global
state."""
- global app, auth_manager
- app = None
- auth_manager = None
+ cached_app.cache_clear()
+ _AuthManagerState.instance = None
def get_auth_manager_cls() -> type[BaseAuthManager]:
@@ -150,10 +149,9 @@ def get_auth_manager_cls() -> type[BaseAuthManager]:
def create_auth_manager() -> BaseAuthManager:
"""Create the auth manager."""
- global auth_manager
auth_manager_cls = get_auth_manager_cls()
- auth_manager = auth_manager_cls()
- return auth_manager
+ _AuthManagerState.instance = auth_manager_cls()
+ return _AuthManagerState.instance
def init_auth_manager(app: FastAPI | None = None) -> BaseAuthManager:
@@ -171,12 +169,12 @@ def init_auth_manager(app: FastAPI | None = None) ->
BaseAuthManager:
def get_auth_manager() -> BaseAuthManager:
"""Return the auth manager, provided it's been initialized before."""
- if auth_manager is None:
+ if _AuthManagerState.instance is None:
raise RuntimeError(
"Auth Manager has not been initialized yet. "
"The `init_auth_manager` method needs to be called first."
)
- return auth_manager
+ return _AuthManagerState.instance
def init_plugins(app: FastAPI) -> None: