Skip to content

Reset last_scheduling_decision when marking a Dag run as queued - #77603

Open
seunggabi wants to merge 2 commits into
apache:mainfrom
seunggabi:fix/mark-queued-reset-scheduling-decision
Open

seunggabi wants to merge 2 commits into
apache:mainfrom
seunggabi:fix/mark-queued-reset-scheduling-decision

Conversation

@seunggabi

Copy link
Copy Markdown
Contributor

Marking a Dag run as queued (UI "Mark run as queued" / PATCH .../dagRuns/{run_id} with state=queued) only changes state. Clearing task instances of a finished run, by contrast, also resets last_scheduling_decision and start_date when the run goes back to queued (clear_task_instances in taskinstance.py).

The scheduler picks queued runs ordered by last_scheduling_decision (nulls first) before run_after (DagRun.get_queued_dag_runs_to_set_running). last_scheduling_decision is only written while a run is running, so a run that was running and is then marked queued keeps an old non-null value, and later runs of the same Dag with a null value are picked first.

With depends_on_past=True and max_active_runs=1 this deadlocks the Dag:

2026-08-24  queued   <- marked queued, keeps last_scheduling_decision, picked last
2026-08-25  running  <- picked first; every task waits for 08-24 (depends_on_past)

The earlier run can never start because the later run holds the only active slot, and the later run's tasks never start because they wait for the earlier run. We hit this on a production deployment (hundreds of Dags stuck in this state after runs were re-queued).

This change resets last_scheduling_decision and start_date when a run is set to queued, matching what clearing does, so queued runs of the same Dag are picked in run_after order.

A unit test covers it; it fails without the change (assert dr.last_scheduling_decision is None) and passes with it.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: Claude Code following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

Clearing task instances of a finished run resets last_scheduling_decision
and start_date when the run goes back to queued, but marking a run as
queued through the API or UI only changed its state. The scheduler orders
queued runs by last_scheduling_decision (nulls first) before run_after, so
a run that was running and then marked queued kept its old value and could
be picked after later runs of the same Dag. With depends_on_past and
max_active_runs=1 the later run then waits for the earlier one forever.
session.scalar returns DagRun | None, so reassigning it to the DagRun variable failed mypy. Load the run into a separate variable and assert it is not None, like the other tests in this file.

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:API Airflow's REST/HTTP API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant