-
Notifications
You must be signed in to change notification settings - Fork 17.1k
Purge heartbeat-timed-out tasks whose last_heartbeat_at is still NULL #66773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Detect task instances stuck in ``RUNNING`` that never sent a first heartbeat. The scheduler's heartbeat-timeout query previously used ``last_heartbeat_at < limit_dttm``, which SQL evaluates to NULL for rows that have never heartbeated, so those task instances were silently skipped and stayed ``RUNNING`` forever. The query now falls back to ``start_date`` when ``last_heartbeat_at`` is NULL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you walk me through how
TI.last_heartbeat_atcan be null for a running task as I thought it's set here:airflow/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py
Line 237 in 07bf075
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update 2026-05-19: the timing assumption in this argument was wrong. Live repro on a freshly-migrated A3 deployment showed
adopt_or_reset_orphaned_tasksfires within ~14 ms of scheduler startup, rotates the NULL-heartbeat TIs intotask_instance_history, and_find_task_instances_without_heartbeatsonly ever runs against the fresh rows that already havelast_heartbeat_atpopulated by/run. The cleanup query never observes the NULL state, so the predicate this PR adds is unreachable on a normal restart path. See the close-comment for the trace and the real-bug hypothesis on #58307.Original argument, preserved for thread continuity
Fair point — let me walk through it. The
/runendpoint you linked does setstate=RUNNINGandlast_heartbeat_at=utcnow()atomically in the sameUPDATE, so in steady-state Airflow 3 a freshly-running TI is not NULL on that path.The case I'm targeting is the Airflow 2 → 3 upgrade legacy state. Migration
0045_3_0_0_add_last_heartbeat_at_directly_to_tiadds the column asnullable=Truewithout a backfill. Any TI that was already RUNNING at upgrade time haslast_heartbeat_at IS NULLuntil something writes to it. The codebase already acknowledges this exact state insideadopt_or_reset_orphaned_tasks:adopt_or_resetonly runs at scheduler startup / on the scheduler-lock timer._find_task_instances_without_heartbeatsruns on a tighter loop and currently has no matching fallback — a TI in the migration state is invisible to the cleanup query and stays RUNNING forever. This PR is the heartbeat-cleanup-path counterpart to the existingadopt_or_resetfallback.Captured the regression deterministically — reverting the new
or_(...)predicate and rerunning the regression test onmain:With the fix in place, the test passes — and the companion
..._null_last_heartbeat_fresh_startcase pins that a newly-started TI inside its first timeout window is left alone. Updated the PR body with the full before/after snippet and the steady-state walkthrough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, this doesn't match your PR description and even at that, at scheduler startup after upgrade, the adoption happens and is scheduled to repeat at intervals. This scenario you painted here won't happen in real life.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circling back — you're right, @ephraimbuddy. Closing this; the live repro shows this isn't the right fix for #58307.
Ran the repro on a freshly-migrated A3 sqlite deployment —
airflow db migrate, a real DAG,airflow standalone. Triggered, two TIs reached running through/run. Stopped the scheduler, setlast_heartbeat_at = NULLon both rows to simulate the post-migration state, restarted with DEBUG logging.adopt_or_reset_orphaned_tasksrotates both intotask_instance_historywithin 32 ms of startup; fresh rows come up withlast_heartbeat_atpopulated by/runin 4 s. The cleanup query only ever sees the fresh rows, so theOR last_heartbeat_at IS NULLpredicate never fires.The repro did surface something different in #58307. vgl-grin is on
heartbeat_sec=0,heartbeat_timeout=300, containers respawning between runs. Theirlast_heartbeat_atisn't NULL;/runsets it. But the adopt path on each respawn looks like it can land a freshlast_heartbeat_at, which would restart the timeout clock indefinitely. That fits the asymmetry —timeout=1or2terminate,300/4/5/6/10don't. Different code path than this PR — leaving #58307 open.Thanks for the pushback.