Skip to content

on_failure_callback executed when task with retries is killed externally #56196

Closed

Description

@wolfier

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

  1. Execute a long running task
  2. Terminate the worker
  3. Allow the scheduler to identify the task instance was killed externally
  4. Examine dag processor logs for execution of the task'son_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_callback

The 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

Activity

  1. added
    kind:bug
    needs-triage
    on Sep 29, 2025
  2. amoghrajesh commented on Sep 29, 2025

    @amoghrajesh
    Contributor

    Good investigation @wolfier. Let me assign it to myself.

  3. removed
    needs-triage
    on Sep 29, 2025
  4. self-assigned this
    on Sep 29, 2025
  5. added
    affected_version:3.1
    on Sep 29, 2025
  6. eladkal commented on Oct 1, 2025

    @eladkal
    Contributor

    I was pretty sure I already commented on this issue (?) maybe I just prepared a comment and didn't hit submit 😕

    When a task with remaining retries fails, the on_retry_callback should be executed and NOT on_failure_callback.

    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_callback must be executed so the issue here is not if on_failure_callback should be executed - the answer to that is always yes. The issue here is if on_retry_callback should be called as well.

    Let me demonstrate an example:

    from airflow import DAG
    from airflow.decorators import task
    from airflow.exceptions import AirflowFailException
    from datetime import datetime
    
    def on_failure_callback(context):
        print("Task failed")
    
    def on_retry_callback(context):
        print("Task retrying")
    
    @task(retries=3, on_failure_callback=on_failure_callback, on_retry_callback=on_retry_callback)
    def failing_task():
        raise AirflowFailException("Task failed")
    
    dag = DAG(
        "test_dag",
        start_date=datetime(2024, 1, 1),
        schedule=None,
    )
    
    failing_task()
    

    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_callback to be called?

  7. wolfier commented on Oct 2, 2025

    @wolfier
    ContributorAuthor

    What would you expect to happen in this case?

    If the task instance in the above case fails, the task state will move directly to failed and when finalized the on_failure_callback will 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.

    there are 3 retries but no retries will ever be invoked. Do you expect on_retry_callback to be called?

    I do not expect on_retry_callback to be invoked.


    There are two distinct code paths.

    • The task execution command fails unexpectedly which is generally considered as the task instance killed externally. This leads to the scheduler sending the on_failure_callback to the dag processor to be invoked.
      • If on_failure_callback and on_retry_callback is set, the on_failure_callback is invoked.
      • If on_failure_callback is set and on_retry_callback is not set, the on_failure_callback is invoked.
      • If on_failure_callback is not set and on_retry_callback is set, a warning is printed. (I believe)
    • The task execution command completes successfully and the execution results in an exception. This leads to the worker resolving the task instance state and finalizing the task instance which includes executing callbacks.

    Currently in Airflow 3.1, when the task execution command fails unexpectedly, the on_failure_callback is invoked. If the task instance is eligible for retry, the task will be retried. I believe the behaviour should be the on_retry_callback is executed if present otherwise no callback is invoked since the task did enter the failed state.

    You describe a case where the task is killed. Killed = task goes to failure state which means that on_failure_callback must be executed so the issue here is not if on_failure_callback should be executed - the answer to that is always yes.

    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 failed state. Since the task instance is eligible for retry, the task instance state is set to up_for_retry.

    The issue here is if on_retry_callback should be called as well.

    I think only the on_retry_callback should be executed if the task is eligible for retries. If the task is not eligible for retires, the current behaviour is sufficient though the implied on_failure_callback execution could be confusing.

        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.FAILED,   # Explicitly denote the task callback type
        )
  8. eladkal commented on Oct 2, 2025

    @eladkal
    Contributor

    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.

    Ah ok!
    Now I get the issue.
    What you are proposing make sense

  9. added this to the Airflow 3.1.1 milestone on Oct 13, 2025
  10. added theissue type on Oct 13, 2025
  11. added a commit that references this issue on Oct 13, 2025
    888f588
  12. assigned and unassigned on Oct 13, 2025
  13. kaxil commented on Oct 13, 2025

    @kaxil
    Member

    PR: #56586

  14. added 2 commits that reference this issue on Oct 14, 2025
    e18274a
  15. added a commit that references this issue on Oct 14, 2025
    8af7e9a
  16. added a commit that references this issue on Oct 22, 2025
    2c0d01a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

affected_version:3.1area:corekind:bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions