Skip to content

Document that on_task_instance_failed also fires for retried attempts - #72424

Merged
kaxil merged 6 commits into
apache:mainfrom
avanish-garg:feature/task-instance-up-for-retry-listener-hook-72379
Nov 20, 2026
Merged

kaxil merged 6 commits into
apache:mainfrom
avanish-garg:feature/task-instance-up-for-retry-listener-hook-72379

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

related: #72426

on_task_instance_failed fires for an attempt that will be retried as well as for a final failure. In both the scheduler path (fetch_handle_failure_context) and the task runner (finalize) the task instance is already UP_FOR_RETRY when the hook runs, so a listener can tell the two apart with task_instance.state and doesn't need a new hook (as discussed above).

The documentation didn't say so: the hookspec docstring read "Execute when task state changes to FAIL". This PR only changes docs:

  • the hookspec docstring for on_task_instance_failed
  • a paragraph under on_task_instance_failed in the listeners guide
  • the example listener, which now checks task_instance.state

The on_task_instance_up_for_retry hook this PR first proposed is removed.

Not run: a Sphinx docs build and the unit tests (no Airflow environment here); ruff check and format are clean on the changed Python files.

Change

  • New on_task_instance_up_for_retry(previous_state, task_instance, error) hookspec in the shared
    listener spec (airflow_shared/listeners/spec/taskinstance.py), picked up automatically by both
    the core and task-sdk listener managers (no separate registration needed, unlike specs that live
    in their own module).
  • Fired from both existing on_task_instance_failed call sites, right after that call, only when
    the task instance's resulting state is UP_FOR_RETRY:
    • models/taskinstance.py::TaskInstance.fetch_handle_failure_context (API-server-triggered path)
    • execution_time/task_runner.py's finalize() (task-execution path — this one already has a
      distinct on_retry_callback vs on_failure_callback split at the DAG-author callback level,
      so this hook follows the same precedent already established for user-facing callbacks)
  • on_task_instance_failed is left firing unchanged in both branches — this is purely additive,
    so no existing listener's behavior changes.
  • Docs: new bullet + example in administration-and-deployment/listeners.rst's "Compatibility"
    table and TaskInstance events section, plus a runnable example in
    example_dags/plugins/event_listener.py.
  • Tests covering both call sites: that the new hook fires for the retry-eligible branch, that it
    does not fire for the terminal-failure branch, and that on_task_instance_failed still fires
    in both cases as before.

Design note

I considered instead scoping on_task_instance_failed to the terminal-failure branch only (as
suggested in the issue), but that would be a breaking behavior change for any listener already
relying on it firing for retries too. Adding the new hook without touching the existing one's
firing behavior is a strictly additive, backward-compatible change, so I went with that instead.
Happy to discuss if a different tradeoff is preferred.

Fired when a task instance is set to UP_FOR_RETRY after a failure, so
listeners can distinguish a task's automatic retries from its terminal
failures without inspecting task_instance.state themselves.
on_task_instance_failed continues to fire for both cases, unchanged,
for backward compatibility.
@avanish-garg

Copy link
Copy Markdown
Contributor Author

For reviewers: this touches taskinstance.py's failure-handling path (fetch_handle_failure_context) and the shared listener spec. cc @henry3260 @guan404ming @dheerajturaga — I see each of you has prior commits in that file, so flagging in case this area is useful for you to weigh in on. cc @potiuk as well, given the listener-registration surface this also touches (same as #36516).

@avanish-garg avanish-garg mentioned this pull request Sep 2, 2026
2 tasks done
@avanish-garg
avanish-garg marked this pull request as ready for review September 9, 2026 11:30
@kaxil

kaxil commented Oct 1, 2026

Copy link
Copy Markdown
Member

What's the case on_task_instance_failed doesn't cover? By the time it fires for a retry, the task instance's state is already UP_FOR_RETRY, in both the scheduler path (fetch_handle_failure_context sets it before notify_failure) and the task runner (finalize calls the hook from its UP_FOR_RETRY branch). So an alerting listener can skip retries by checking task_instance.state, and Dag authors also have on_retry_callback. A new hookspec is permanent public surface for every listener implementation. Is there a listener that can't use the state check? Converting to draft until there is.

@kaxil
kaxil marked this pull request as draft October 1, 2026 21:21
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Nov 16, 2026
…ce-failed-retry-note

# Conflicts:
#	airflow-core/src/airflow/models/taskinstance.py
The hook runs when an attempt will be retried as well as when it fails for good. task_instance.state is UP_FOR_RETRY in the first case and FAILED in the second, so a listener can tell them apart without a new hook. Say so in the hookspec docstring, the listeners guide and the example listener, and remove the on_task_instance_up_for_retry hook this PR first added.
@avanish-garg
avanish-garg marked this pull request as ready for review November 20, 2026 09:24
@avanish-garg

Copy link
Copy Markdown
Contributor Author

Thanks, and sorry for the late answer @kaxil You're right and I've cut this PR down to a docstring change for on_task_instance_failed (fires for retries too, check task_instance.state for UP_FOR_RETRY vs FAILED) and drop the new hook!

@avanish-garg avanish-garg changed the title Add on_task_instance_up_for_retry listener hook Document that on_task_instance_failed also fires for retried attempts Nov 20, 2026
@kaxil kaxil closed this Nov 20, 2026
@kaxil kaxil reopened this Nov 20, 2026
@kaxil
kaxil enabled auto-merge (squash) November 20, 2026 12:23
@kaxil
kaxil merged commit a605c7a into apache:main Nov 20, 2026
70 checks passed
@boring-cyborg

boring-cyborg Bot commented Nov 20, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk kind:documentation stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants