Skip to content

Honour retries when failing tasks stuck in queued - #76908

Open
hkc-8010 wants to merge 7 commits into
apache:mainfrom
hkc-8010:fix/stuck-queued-honour-retries
Open

hkc-8010 wants to merge 7 commits into
apache:mainfrom
hkc-8010:fix/stuck-queued-honour-retries

Conversation

@hkc-8010

@hkc-8010 hkc-8010 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

When a task has been requeued num_stuck_in_queued_retries times and is still stuck in queued, _maybe_requeue_stuck_ti sets it to failed with ti.set_state(FAILED). That skips retries entirely. On Airflow 3 the DAG processor callback only runs user callbacks and never touches state, so the task stays failed for good. The callback request also had no task_callback_type, so on_failure_callback fired even when retries were left.

This changes the tries-exceeded branch to do what the heartbeat-timeout purge already does:

  • load the task and set ti.task, so is_eligible_to_retry(), retry_delay and fail_fast work
  • send the TaskCallbackRequest with UP_FOR_RETRY or FAILED, built from the failed attempt before it is archived
  • send an EmailRequest from here, because the executor event for this TI no longer reaches the path in process_executor_events that sends one
  • call ti.handle_failure() instead of set_state(FAILED), then executor.fail() with the failed attempt's key

The stale executor event is dropped as before: on retry the old row is archived, and with no retries left the TI is already failed, not queued.

How this showed up

On 2.11.2 the same set_state(FAILED) produces a different symptom. There the DAG processor's callback does call handle_failure(), so the task does retry a few seconds later. But in that gap the scheduler sees a failed upstream and marks all_success downstream tasks upstream_failed, and nothing re-evaluates them after the retry succeeds. The run then finishes success with 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 a failed window.

On main there is no gap, because nothing ever retries the task.

Behaviour changes

  • Tasks failed for being stuck in queued now send email when email_on_failure / email_on_retry is set. Nothing sent one before.
  • handle_failure() writes the usual failed Log row, increments the failure stats and fires on_task_instance_failed. Listeners such as OpenLineage now see a FAILED event for these tasks, as they would for any other failure.
  • With retries left, the old executor event is discarded with the existing "no longer current" warning.

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 to fail(), and with a retry pending the downstream task is not upstream_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.
  • Two existing tests now expect the extra failed Log 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.py passes 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?
  • Yes (please specify the tool below)

Generated-by: Claude Code following the guidelines

@hkc-8010
hkc-8010 marked this pull request as ready for review November 11, 2026 10:53
@eladkal eladkal added this to the Airflow 3.4.1 milestone Nov 11, 2026
@eladkal eladkal added type:bug-fix Changelog: Bug Fixes backport-to-v3-4-test Backport to v3-4-test labels Nov 11, 2026
@eladkal
eladkal requested a review from amoghrajesh November 11, 2026 10:59

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will create a race condition imo.

@hkc-8010
hkc-8010 requested a review from amoghrajesh November 11, 2026 11:35

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add to the PR body: stuck tasks with email_on_failure now send email

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me now, but I would request @ashb for another look to be sure

hkc-8010 and others added 5 commits November 28, 2026 10:51
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@vatsrahul1001

Copy link
Copy Markdown
Contributor

@ashb could use a review

Looks fine to me now, but I would request @ashb for another look to be sure

@ashb could use a review here

This branch has not been deployed

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

Labels

area:scheduler backport-to-v3-4-test Backport to v3-4-test type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants