Skip to content

Scheduler exits when handle_failure raises for a single task instance #76529

Open

Description

@ChickenBenny

Under which category would you file this issue?

Airflow Core

Apache Airflow version

main; the same code path is present in 3.3.2

What happened and how to reproduce it?

Follow-up from #76191 (fixing #76190).

The scheduler calls ti.handle_failure() for each task instance inside a loop, with nothing that isolates an exception raised for one task instance:

  • SchedulerJobRunner.process_executor_events, when the executor reports a failure the TI did not record itself
  • the heartbeat-timeout purge (_purge_task_instances_without_heartbeats)

If handle_failure() raises for any reason, the exception leaves _run_scheduler_loop and the scheduler process exits. After a restart the same task instance is still in the same state, so it is picked up again, raises again, and the scheduler keeps crashing. In #76190 this was a KeyError from fail_fast handling. A task with retries=0 reached try 13 in about 30 minutes, because each restart reset the TI as orphaned and ran it again.

#76191 removes that specific KeyError, but any other exception raised from handle_failure() (a failure callback lookup, fail_fast handling, a data problem in one Dag run) has the same effect. One bad task instance can stop scheduling for every Dag.

To reproduce, make handle_failure() raise for one task instance. For example, on a build without #76191, follow the steps in #76190.

What you think should happen instead?

An exception from handle_failure() for one task instance should not stop the scheduler. It should log the error with the task instance key, and leave the TI in a state that does not cause it to be picked up again in the same way, for example FAILED without the extra handling. It should then continue with the remaining task instances.

Open points:

  • If the exception comes from the database, the transaction may no longer be usable. Handling each TI inside a savepoint (session.begin_nested()) may be needed so that one TI's failure does not roll back the others.
  • Which fallback state to use when handle_failure() itself cannot complete.

Operating System

No response

Deployment

None

Apache Airflow Provider(s)

No response

Versions of Apache Airflow Providers

No response

Official Helm Chart version

Not Applicable

Kubernetes Version

No response

Helm Chart configuration

No response

Docker Image customizations

No response

Anything else?

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 Nov 5, 2026
  2. its-amann commented on Nov 5, 2026

    @its-amann

    Hi! I'd like to work on this. Could you assign it to me? I'll follow up with a PR that includes a fix and tests.

  3. ChickenBenny commented on Nov 5, 2026

    @ChickenBenny
    ContributorAuthor

    @its-amann Sure, go ahead! Note that Airflow doesn't assign issues to contributors. See How to contribute. Just open a PR with the fix when it's ready.

    One heads-up: this touches the same code as #76191, so it will probably need to wait until that one is merged (or be rebased on top of it).

  4. its-amann commented on Nov 5, 2026

    @its-amann

    Opened a fix for this in #76544, would appreciate a look when you get a chance.

  5. manucorujo commented on Nov 9, 2026

    @manucorujo
    Contributor

    Another way to reach this on the released 3.4.0: a race between the executor event and the worker's own state update. The scheduler exited once with a ValueError from TaskInstance.archive().

    Timeline for one task instance (ti_id=01a1c13b-5a3e-..., try 2, KubernetesExecutor, retries left):

    • 16:57:59.690: the executor sees the pod fail (Pod phase: Failed).
    • 16:58:07.465: the scheduler processes the event: reported that the task instance ... [running] ... finished with state failed, but the task instance's state attribute is running. It calls handle_failure().
    • 16:58:30.248: the worker's PATCH /execution/task-instances/{id}/state commits on the API server and archives the same row for the retry (up_for_retry, host_name = that task pod). The API server was slow at that time: PATCH /state took up to 116 s, so this request finished about 30 s after the pod had stopped.
    • 16:58:30.352: the scheduler exits:
    File ".../airflow/jobs/scheduler_job_runner.py", line 1813, in process_executor_events
        ti.handle_failure(error=msg, session=session)
    File ".../airflow/models/taskinstance.py", line 2826, in fetch_handle_failure_context
        ti = ti.prepare_db_for_next_try(session)
    File ".../airflow/models/taskinstance.py", line 1786, in prepare_db_for_next_try
        self.archive(reason="retry", session=session)
    File ".../airflow/models/taskinstance.py", line 1695, in archive
        raise ValueError("An archived task instance cannot be archived again")
    

    Our reading: archive() takes SELECT ... FOR UPDATE on the row, waits for the API server transaction that archives the same row, and then finds working_set false. Before 3.4.0 the same double handle_failure path did not raise.

    #76544 would stop the scheduler from exiting. It may also be worth a check in process_executor_events that skips handle_failure() when the row is no longer in the working set, so that the retry is not prepared twice.

    It happened once. The scheduler did not crash again after the restart.


    Drafted-by: Claude Code (Opus 5.5); reviewed by @manucorujo before posting

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions