airflow
airflow copied to clipboard
Move the remaining sync DB lookups off the API server event loop
Body
Context
- An
async defFastAPI dependency that runs a sync query waits for a pooled connection on the event loop. When the pool is full, the loop blocks, and the requests holding connections can't release them, because their teardown needs the loop too. That stalled 3.4.0rc2 under concurrent writes (#76417, found in #75865 (comment)). - #76416 fixes the 3.4.0 regression in
log_action. #76424 adds tests that fail on sync DB work on the event loop, with the known offenders listed as strict xfails ingenerated/. - The items below are the same pattern. They were already in 3.3.2 and are off the common path, so they are 3.4.1 follow-ups, not RC blockers. Under load each one still wedges the whole server: a single call per second is enough once the pool is busy.
Follow-up items
- [ ]
requires_access_backfillincore_api/security.py: looks upBackfill.dag_idon/backfills/{backfill_id}and its/dag_runs,/pause,/unpauseand/cancelroutes.DagModel.get_team_namealso runs on the loop when its cache misses. PRs: #76493, backport #77895. - [ ]
requires_access_event_logincore_api/security.py: looks up theLogrow onGET /eventLogs/{event_log_id}. PR: #76583. - [ ]
_collect_teams_to_checkincore_api/security.py(only with[core] multi_team = True): calls the syncPool,ConnectionorVariableget_team_nameon detail, update and delete routes. It also callsTeam.get_name_if_existswhen a create or update body setsteam_name. PRs: #76515, backport #77840. - [ ]
BaseAuthManager.get_user_from_token: replacerun_in_threadpool(RevokedToken.is_revoked, ...)with an async query. It's listed ingenerated/known_api_server_threadpool_offloads.txt. PR: #76963. - [x]
GET /api/v2/dags:get_dagspasses the lazysession.scalars(...)intoIterable[DAGResponse], so the rows and thedag_tagquery are read while FastAPI serializes the response on the loop. Materialize it with.all(), and check the other list routes whose collection field isIterable[...]. #74247 fixed the same shape for Dag details and pools. PRs: #76525 and #76537, backports #77765 and #77864. - [x] FAB provider:
FabAuthManager.deserialize_userruns a sync user query fromget_user_from_tokenon the loop each time its per-user cache ([fab] cache_ttl) expires. PR: #76748. - [ ] Coverage (
test_event_loop_blocking.py): the sweep sends"1"for every path parameter and{}as the body against an empty DB. Routes that reject that input early, or that only query when rows exist (such as theGET /dagscase), are not covered. The mounted/executionapp isn't covered either, because the sweep's UI token is rejected there before any route dependency runs.
How to fix
- Run the lookup on the async engine instead of the sync session. Take
AsyncSessionDep, or useasync with create_session_async() as session:, andawait session.scalar(...), as #76416 does forlog_action. Add async variants of the sync@provide_sessionhelpers where needed. Don't wrap the call inrun_in_threadpool:TestNoThreadpoolOffloadfails on any new offload. - Delete the fixed routes' lines from
generated/known_sync_db_on_event_loop.txt, or fromgenerated/known_api_server_threadpool_offloads.txtforis_revoked. Each line is a strict xfail, so the run fails until the line is removed.
Committer
- [x] I acknowledge that I am a maintainer/committer of the Apache Airflow project.
@jason810496 I'll take "requires_access_event_log in core_api/security.py: looks up the Log row on GET /eventLogs/{event_log_id}."
I'll takerequires_access_backfill, thank you!
I'll pick up _collect_teams_to_check one, thanks!
I'll take the GET /api/v2/dags item, including checking the other list routes whose collection field is Iterable[...]. Thanks!
I'll work on the BaseAuthManager.get_user_from_token one.
I'll work on FAB provider. Thanks