Repository navigation
Conversation
amoghrajesh
left a comment
There was a problem hiding this comment.
This will create a race condition imo.
amoghrajesh
left a comment
There was a problem hiding this comment.
Please also add to the PR body: stuck tasks with email_on_failure now send email
amoghrajesh
left a comment
There was a problem hiding this comment.
Looks fine to me now, but I would request @ashb for another look to be sure
Once a task exceeds num_stuck_in_queued_retries the scheduler set it to FAILED directly. The DAG processor callback only runs user callbacks, so the task never retried, and on_failure_callback fired even when retries remained. Route the failure through handle_failure() and send a typed callback and email request, as the heartbeat-timeout path already does.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
2e551e8 to
ad77c97
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The change can unexpectedly commit unrelated caller-session updates on skipped candidates and cause deadlocks when fail-fast handling updates mapped siblings without first acquiring the DagRun lock.
3 open issues
What changed in this PR
This PR makes queued-timeout failures honour task retries through Airflow’s normal failure handling, avoiding premature terminal failure and downstream task propagation.
Changes:
- Uses
handle_failure()and preserves the failed attempt’s identity for callbacks, email requests, and executor cleanup. - Shares failure-notification logic across scheduler paths and adds locking checks before handling queued tasks.
- Adds regression tests for retries, notifications, unavailable Dags, request failures, and task-state races; updates expected failure logs.
| File | Description |
|---|---|
airflow-core/tests/unit/jobs/test_scheduler_job.py |
Adds queued-timeout regression coverage and updates log expectations. |
airflow-core/src/airflow/jobs/scheduler_job_runner.py |
Applies retry-aware failure handling, shared notifications, and queued-task locking checks. |
Review effort: Balanced
Failing a fail_fast task also updates its sibling TIs, so an unmapped stuck TI needs the same DagRun-first lock order as regional ones to avoid deadlocking with a clear (apache#74349). Adds a fail_fast regression test. Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>


When a task has been requeued
num_stuck_in_queued_retriestimes and is still stuck in queued,_maybe_requeue_stuck_tisets it tofailedwithti.set_state(FAILED). That skipsretriesentirely. On Airflow 3 the DAG processor callback only runs user callbacks and never touches state, so the task staysfailedfor good. The callback request also had notask_callback_type, soon_failure_callbackfired even when retries were left.This changes the tries-exceeded branch to do what the heartbeat-timeout purge already does:
ti.task, sois_eligible_to_retry(),retry_delayandfail_fastworkTaskCallbackRequestwithUP_FOR_RETRYorFAILED, built from the failed attempt before it is archivedEmailRequestfrom here, because the executor event for this TI no longer reaches the path inprocess_executor_eventsthat sends oneti.handle_failure()instead ofset_state(FAILED), thenexecutor.fail()with the failed attempt's keyThe stale executor event is dropped as before: on retry the old row is archived, and with no retries left the TI is already
failed, notqueued.How this showed up
On 2.11.2 the same
set_state(FAILED)produces a different symptom. There the DAG processor's callback does callhandle_failure(), so the task does retry a few seconds later. But in that gap the scheduler sees a failed upstream and marksall_successdownstream tasksupstream_failed, and nothing re-evaluates them after the retry succeeds. The run then finishessuccesswith those tasks skipped. This was reproduced with the docker-compose setup on 2.11.2 (worker concurrency 1,task_queued_timeout=60, one task holding the only slot). It did not reproduce on 2.8.4, where the timeout went through the executor-event failure path and never left afailedwindow.On main there is no gap, because nothing ever retries the task.
Behaviour changes
email_on_failure/email_on_retryis set. Nothing sent one before.handle_failure()writes the usualfailedLog row, increments the failure stats and fireson_task_instance_failed. Listeners such as OpenLineage now see a FAILED event for these tasks, as they would for any other failure.Tests
test_stuck_in_queued_tries_exceeded_honours_retries(0 and 1 retries): final state, callback type, email type, failed attempt id and try number in both requests, the executor key passed tofail(), and with a retry pending the downstream task is notupstream_failed.test_stuck_in_queued_tries_exceeded_retries_without_callbacks/..._when_dag_not_loadable: retries still happen with no callbacks, and when the DAG can't be loaded.test_stuck_in_queued_skips_task_started_after_scan: a TI a worker started after the scan is left alone.test_stuck_in_queued_locks_dag_run_before_ti(unmapped and mapped): the DagRun is locked before the TI, and the TI is skipped while the DagRun is held.failedLog row.Reverting the source change fails 6 tests. Dropping only the callback type, only the email, or putting
set_state(FAILED)back each fails at least 2.test_scheduler_job.pypasses on Python 3.11 (613 passed). The stuck-in-queued, heartbeat and executor-event tests also pass on Postgres 14. The lock tests only prove the order and the skip; SQLite can't show real contention.Added a significant newsfragment listing the behaviour changes.
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code following the guidelines