Repository navigation
Description
Problem
Under high concurrency (80+ simultaneous task completions emitting asset events), the Airflow 3 API server dies with OOMKill. The root cause is a DB lock contention chain:
ti_update_state()acquiresSELECT task_instance ... WITH FOR UPDATE, holding a PostgreSQL row lock.- While holding that lock,
register_asset_changes_in_db()callsAssetManager.register_asset_change(), which runs multiple slow queries includingasset_alias_model.asset_events.append(asset_event). This ORM .append() lazy-loads the entireasset_eventscollection for the alias -- potentially thousands of rows on long-running deployments. - Each slow query leaves the connection
idle in transactionwhile Python processes results. New workers needingSELECT task_instance FOR UPDATEon the same row queue up, each holding a FastAPI threadpool thread via Cadwyn'srun_in_threadpool. - With 80+ concurrent completions, thread count grows unbounded until OOMKill.
DB evidence (confirmed from production, May 2026)
pid 31104: idle in transaction, xact_age=3:35
last query: SELECT asset_alias WHERE name IN ('fivetran-synced-table')
pid 31303: idle in transaction, xact_age=3:31, same pattern
-- both blocking: SELECT task_instance FOR UPDATE WHERE id = UUID(...)
Disabling the trigger DAGs dropped apiserver memory from 5Gi+ to MBs instantly. Re-enabling just one DAG reproduced 76 QueryCanceled: canceling statement due to statement timeout errors in 1.5 hours.
Blocking query chain confirmed from pg_stat_activity:
pid 31303: idle in transaction
INSERT INTO asset_alias_asset_event VALUES (2, 1151718)
pid 31104: idle in transaction
SELECT asset_alias ... WHERE name IN ('fivetran-synced-table')
-- both blocking 8 workers doing:
SELECT task_instance ... FOR UPDATE WHERE id = UUID(...)Fix
Two changes:
1. AssetManager.register_asset_change() (assets/manager.py): Replace asset_alias_model.asset_events.append(asset_event) + session.add(asset_alias_model) with a direct INSERT INTO asset_alias_asset_event (alias_id, event_id). This eliminates the lazy-load of the existing events collection (which can be thousands of rows) while the task_instance row lock is held.
2. ti_update_state() (execution_api/routes/task_instances.py): Add session.commit() after the TI state UPDATE and Log writes to release the task_instance row lock before running asset registration. Asset registration then runs in a fresh implicit transaction. Registration failures are logged and swallowed -- the task state is already durable at that point.
Note on session.commit() in a session-parameter function: this intentionally deviates from the project convention. No code after the commit relies on rollback; the subsequent session.get() re-loads fresh state. Alternative approaches (second session, background task) were considered but have higher operational complexity for equivalent correctness.
Testing
- New:
test_register_asset_change_with_alias_no_lazy_load-- confirms no SELECT onasset_alias_asset_eventcollection during registration when pre-existing rows exist - New:
test_ti_update_state_to_success_asset_registration_failure_returns_204-- confirms 204 + TI SUCCESS when asset registration raises after commit - All 22 existing
test_manager.pytests pass - All 38 existing
TestTIUpdateStatetests pass - Breeze
--python 3.10 --db-resetclean on both test files
Are you willing to submit a PR?
- Yes I am willing to submit a PR!
Activity
Register asset events before taking the task_instance row lock
Document Airflow 3 asset alias slowdown (#3200)