Repository navigation
Record TaskInstanceHistory when a task fails from a non-running state - #67372
Conversation
7d3f9d0 to
190c0b6
Compare
This comment was marked as spam.
This comment was marked as spam.
|
@sagu2602 Yes — your scenario (attempt-4 fails while QUEUED, logs tab missing that attempt) is exactly what this PR fixes. When a task fails from With this fix |
Thanks for the detailed review — you're exactly right. The root cause is in # Before
if ti.state == TaskInstanceState.RUNNING:
ti.prepare_db_for_next_try(session)When the executor sends a FAILED event for a QUEUED/SCHEDULED/DEFERRED TI, Fixed by changing the guard to exclude only # After
if ti.state != TaskInstanceState.RESTARTING:
# Covers RUNNING (normal exception), QUEUED/SCHEDULED/DEFERRED (external kill)
ti.prepare_db_for_next_try(session)Added a parametrized regression test |
c9acbc2 to
5bab7ed
Compare
# Conflicts: # airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py # airflow-core/src/airflow/models/taskinstancehistory.py
shahar1
left a comment
There was a problem hiding this comment.
Could you please think of another title for this PR?
The issue might be surfaced in KubernetesExecutor, but the fix is not in the Kubernetes provider - which is rather confusing.
@shahar1 Good point — retitled to "Record TaskInstanceHistory when a task fails from a non-running state". |
potiuk
left a comment
There was a problem hiding this comment.
Approving — the condition change is correct and the regression test is a real one. Three non-blocking cleanups inline that I'd like you to take a look at before this goes in; none of them changes behaviour.
The parts I went looking for problems in and didn't find any:
- Excluding
RESTARTINGis not optional, and you got it right.is_eligible_to_retry()returnsTrueunconditionally forRESTARTING(taskinstance.py:2000-2003), so a cleared task does reach thiselsebranch — a plain!= RUNNINGwould have recorded its history twice. - No retry is silently consumed.
prepare_db_for_next_try()records history, clearsTaskReschedulerows and re-issues the UUID, but never touchestry_number— that increment lives indagrun.py:2292. Extending the call toQUEUED/SCHEDULEDdoesn't cost an attempt. - A double call would be harmless anyway.
TaskInstanceHistory.record_ti()early-returns when a row already exists for the sametry_number(taskinstancehistory.py:196-206). - Newsfragment present and correctly typed, and the reachable-state set across both
handle_failure()callers is exactly{SCHEDULED, QUEUED, RUNNING}— precisely what the new condition selects.
Smaller observations
Two of the three inline comments are the same point: the test additions in test_task_instances.py and in test_adopt_or_reset_resettable_tasks cover code that was already on main before this PR and pass without your change. The third is a comment listing DEFERRED as a reachable state when it isn't.
One more, with no line to anchor it to: the PR description describes two production changes that aren't in the diff — the record_ti FAILED/end_date forcing (taskinstancehistory.py:207-213) and the Execution API end_date / rendered_map_index snapshot (task_instances.py:683-688). Both were already on main; the actual change here is the five-line condition. Worth trimming the description so the next reader isn't hunting for code that isn't there.
Finally, something to consider rather than change: != RESTARTING is a deny-list against an open enum. It's correct for today's two callers, but a future caller arriving in UP_FOR_RESCHEDULE or with state=None would silently get a history row and a fresh UUID. An explicit in (RUNNING, QUEUED, SCHEDULED) would keep the invariant local and fail safe. Not asking you to change it — just flagging the trade-off.
This review was drafted by an AI-assisted tool and confirmed by an Airflow maintainer. The maintainer approving this PR has read the findings and signed off. If something feels off, please reply on the PR and a maintainer will follow up.
More on how Airflow handles maintainer review: contributing-docs/05_pull_requests.rst.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting
|
@potiuk Thanks — all three applied.
Also trimmed the PR description — it described the |

closes: #65366
closes: #67238
What happened?
When a task instance fails from a non-
RUNNINGstate — e.g. the executor reports FAILED for a TI stillQUEUEDorSCHEDULEDbecause the pod/worker was killed before the task started — the task is retried, but noTaskInstanceHistoryrow is written for the failed attempt. That attempt loses its hostname and dates, so the attempts tab in the UI has no entry and log retrieval fails withhttp://:8793 No host supplied.Why?
fetch_handle_failure_context()only recorded history when the TI was running:handle_failure()has two callers (_process_executor_eventsand the heartbeat purge), and the reachable state set is{SCHEDULED, QUEUED, RUNNING, RESTARTING}. So a QUEUED or SCHEDULED failure skippedprepare_db_for_next_try()— and thereforeTaskInstanceHistory.record_ti()— entirely.What changed?
The guard now excludes only
RESTARTING(task cleared via the UI/API, whereprepare_db_for_next_try()already ran as part of the clear), so RUNNING, QUEUED and SCHEDULED all record their attempt:Plus a parametrized regression test over
QUEUED/SCHEDULED: after a FAILED executor event the TI must have a new UUID and aTaskInstanceHistoryrow preservinghostnameandstart_date.This is a core-only change — nothing in the Kubernetes provider; KubernetesExecutor is just where the symptom is most visible.