Repository navigation
Description
Apache Airflow version
3.1.0
If "Other Airflow 2 version" selected, which one?
No response
What happened?
on_failure_callback of a task with remaining retry attempts executed when the worker executing the task terminated abruptly. This is not expected because the task has retry attempts.
What you think should happen instead?
When a task with remaining retries fails, the on_retry_callback should be executed and NOT on_failure_callback.
How to reproduce
- Execute a long running task
- Terminate the worker
- Allow the scheduler to identify the task instance was killed externally
- Examine dag processor logs for execution of the task's
on_failure_callback
Operating System
Debian
Versions of Apache Airflow Providers
No response
Deployment
Astronomer
Deployment details
No response
Anything else?
When a task with either on_failure_callback or on_retry_callback is killed externally, a TaskCallbackRequest is sent. The request does not have task_callback_type set and defaults to None. The callback request is considered a failure callback when task_callback_type is None.
Since the task_callback_type is None, the task's on_failure_callback is queued for execution when the dag processor processes the callback request.
if request.task_callback_type is TaskInstanceState.UP_FOR_RETRY:
callbacks = task.on_retry_callback
else:
callbacks = task.on_failure_callbackThe TaskCallbackRequest should set task_callback_type according to whether the task is eligible to retry. If it is, the task should set task_callback_type to TaskInstanceState.UP_FOR_RETRY otherwise TaskInstanceState.FAILED is an appropriate mode.
request = TaskCallbackRequest(
filepath=ti.dag_model.relative_fileloc,
bundle_name=ti.dag_version.bundle_name,
bundle_version=ti.dag_version.bundle_version,
ti=ti,
msg=msg,
context_from_server=TIRunContext(
dag_run=DRDataModel.model_validate(ti.dag_run, from_attributes=True),
max_tries=ti.max_tries,
variables=[],
connections=[],
xcom_keys_to_clear=[],
),
task_callback_type=(
TaskInstanceState.UP_FOR_RETRY
if ti.is_eligible_to_retry() else
TaskInstanceState.FAILED
),
)Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
amoghrajesh commentedon Sep 29, 2025
Good investigation @wolfier. Let me assign it to myself.
eladkal commentedon Oct 1, 2025
I was pretty sure I already commented on this issue (?) maybe I just prepared a comment and didn't hit submit 😕
Yes but this is not the case you are describing.
You describe a case where the task is killed. Killed = task goes to failure state which means that
on_failure_callbackmust be executed so the issue here is not ifon_failure_callbackshould be executed - the answer to that is always yes. The issue here is ifon_retry_callbackshould be called as well.Let me demonstrate an example:
What would you expect to happen in this case?
there are 3 retries but no retries will ever be invoked. Do you expect
on_retry_callbackto be called?wolfier commentedon Oct 2, 2025
If the task instance in the above case fails, the task state will move directly to
failedand when finalized theon_failure_callbackwill be executed.This is not the situation described in the issue as this is the code path where the task execution command completed successfully. The original issue is when the scheduler handles the failure of an externally killed task instance.
I do not expect
on_retry_callbackto be invoked.There are two distinct code paths.
on_failure_callbackto the dag processor to be invoked.on_failure_callbackandon_retry_callbackis set, theon_failure_callbackis invoked.on_failure_callbackis set andon_retry_callbackis not set, theon_failure_callbackis invoked.on_failure_callbackis not set andon_retry_callbackis set, a warning is printed. (I believe)Currently in Airflow 3.1, when the task execution command fails unexpectedly, the
on_failure_callbackis invoked. If the task instance is eligible for retry, the task will be retried. I believe the behaviour should be theon_retry_callbackis executed if present otherwise no callback is invoked since the task did enter thefailedstate.To clarify, it is only the instance of task execution that is "killed". I used the term "killed externally" because that is how Airflow describes the situation. The task instance can be scheduled and queued again if it is eligible for retry. Additionally, the task instance never reached the
failedstate. Since the task instance is eligible for retry, the task instance state is set toup_for_retry.I think only the
on_retry_callbackshould be executed if the task is eligible for retries. If the task is not eligible for retires, the current behaviour is sufficient though the impliedon_failure_callbackexecution could be confusing.eladkal commentedon Oct 2, 2025
Ah ok!
Now I get the issue.
What you are proposing make sense
Fix retry callbacks not executing for externally killed tasks
kaxil commentedon Oct 13, 2025
PR: #56586
[v3-1-test] Fix retry callbacks not executing for externally killed t…
[v3-1-test] Fix retry callbacks not executing for externally killed t…
on_failure_callbackis triggered when a task transitions toup_for_retryafter worker/API server restart #56942 as a duplicate of this issueFix retry callbacks not executing for externally killed tasks (apache…