Skip to content

Release TI lock before asset listener callbacks - #70951

Open
uranusjr wants to merge 4 commits into
apache:mainfrom
astronomer:fix/asset-registration-lock-contention
Open

Release TI lock before asset listener callbacks#70951
uranusjr wants to merge 4 commits into
apache:mainfrom
astronomer:fix/asset-registration-lock-contention

Conversation

@uranusjr

@uranusjr uranusjr commented Aug 2, 2026

Copy link
Copy Markdown
Member

Asset registration on the task-success path (ti_update_state) ran the listener hooks synchronously inside the transaction holding a row lock on the task_instance table. A slow listener, multiplied across a large fan-out of asset events, could hold that lock for minutes, causing statement timeouts.

The listener hooks are now deferred until the end of the endpoint instead of executed inline during asset event creation. Registration writes to the database still happen under the caller's transaction, so durability is unchanged; this only moves the best-effort listener hooks off the lock.

Close #66853.

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comments from my end, LGTM with this direction, thanks.

Comment thread airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py Outdated

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also address the loading issue that #66854 (comment) comment pointed out in this one? Since this PR is small enough. Thanks.

Asset registration on the task-success path (ti_update_state) ran the
listener hooks synchronously inside the transaction holding a row lock
on the task_instance table. A slow listener, multiplied across a large
fan-out of asset events, could hold that lock for minutes, causing
statement timeouts.

The listener hooks are now deferred until the end of the endpoint
instead of executed inline during asset event creation. Registration
writes to the database still happen under the caller's transaction, so
durability is unchanged; this only moves the best-effort listener hooks
off the lock.
@uranusjr
uranusjr force-pushed the fix/asset-registration-lock-contention branch from 1815493 to 1abae3e Compare August 3, 2026 08:59
Comment thread airflow-core/src/airflow/assets/manager.py

@kaxil kaxil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 minor comments lgtm otherwise

# Release the task_instance row lock before running listener callbacks.
session.commit()

for callback in asset_callbacks:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Previously, listeners ran after each event was created. Now their callbacks are collected and run only after all outlets have been processed.

If the second outlet fails with a non-DB error, an event already created for the first outlet can still be committed, but its callbacks are lost because the function never returns them.

(This may be fine since listeners are best effort, just to confirm that this is intentional)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration callback failures cannot bubble here because the hook caller unconditionally wraps them in a try-except (see #70951 (comment)). So all callbacks will always be run unless you kill the server process entirely.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my wording was not clear... I meant a failure during registration itself, before the callbacks are handed back.

For example, if 9 out of 10 outlets are processed and the last one raises a non-DB exception, asset_callbacks here stays empty:

asset_callbacks = ()

try:
    # raises inside, while processing the 10th outlet
    query, updated_state, asset_callbacks = _create_ti_state_update_query_and_update_state(...)
    # so this assignment never happens and asset_callbacks is still ()
except Exception:
    ...  # caught, TI marked FAILED, execution continues

session.commit()  # the 9 events are already in the transaction, so they are committed here

for callback in asset_callbacks:  # empty, the loop never runs
    callback()

So the 9 events are committed, but their listeners are never called. Probably still fine as best effort, just wanted to make sure this case was considered!

@eladkal eladkal added this to the Airflow 3.3.1 milestone Aug 3, 2026
@eladkal eladkal added type:bug-fix Changelog: Bug Fixes backport-to-v3-3-test Backport to v3-3-test labels Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk backport-to-v3-3-test Backport to v3-3-test type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API server OOMKill: task_instance row lock held during asset event emission under high concurrency

6 participants