Skip to content

"Mark failed" returns 500 when a run has task instances for a task no longer in the DAG #72338

Closed

Description

@aeroyorch

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.2.2

What happened and how to reproduce it?

After a few tasks were removed from a DAG, "Mark failed" on an older run returned HTTP 500:

File ".../api/common/mark_tasks.py", line 344, in set_dag_run_state_to_failed
    pending_normal_tis = [ti for ti in pending_tis if not dag.task_dict[ti.task_id].is_teardown]
KeyError: '<task removed in a later DAG version>'

pending_tis is queried without restricting to the DAG's task ids, so it picks up TI rows for
tasks the new version no longer has, and the task_dict lookup blows up. running_tis, built a few
lines above, does apply that filter.

The asymmetry is still there on main, where the code has since been refactored into
_set_dag_run_terminal_state: running_tis has TaskInstance.task_id.in_(task_ids),
pending_tis does not, and the unguarded lookup is at line 300. So it survived that refactor.

How to reproduce

The crash needs a run holding a TI row for a task the resolved DAG version no longer defines.
The path that produced it here:

  1. Remove a task from a DAG and let the dag-processor serialize a new version.
  2. Clear an older run with run_on_latest_version=true. The run and its TIs end up on the new
    version, while the removed task's instance row remains and is reset to NULL.
  3. "Mark failed" on that run returns 500.

Clearing again does not help. SerializedDAG.clear selects every TI for the run regardless of
the version's task set, so it just resets the leftover rows and re-arms the error.

What you think should happen instead?

The two queries should be consistent, and a leftover task instance should be ignored rather than raise.
The scheduler already copes with this: once a run starts, verify_integrity marks such rows removed.

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?

Suggested fix

Add TaskInstance.task_id.in_(task_ids) to the pending_tis query, mirroring running_tis.

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 Aug 31, 2026
  2. KHARSHAVARDHAN-eng commented on Aug 31, 2026

    @KHARSHAVARDHAN-eng

    Hi! @aeroyorch ...I’d be interested in helping investigate and add a regression test for this issue. I’ll first take a closer look at the existing implementation and contribution guidelines to make sure the fix aligns with the project’s expectations...

  3. added
    area:API
    and removed
    needs-triage
    on Sep 2, 2026
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

    area:APIkind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions