Skip to content

Return 404 instead of 500 when ti_run cannot find the DagRun - #72890

Merged
pierrejeambrun merged 4 commits into
apache:mainfrom
astronomer:fix-ti-run-missing-dagrun-404
Sep 10, 2026
Merged

pierrejeambrun merged 4 commits into
apache:mainfrom
astronomer:fix-ti-run-missing-dagrun-404

Conversation

@pierrejeambrun

Copy link
Copy Markdown
Member

The Execution API ti_run route raised a bare ValueError when the DagRun for a task instance could not be found. That try block only guards DataError and SQLAlchemyError, so the ValueError escaped to the app-level Exception handler and the caller received an opaque 500 {"message": "Internal server error"}.

A missing resource should be reported as a 404. This replaces the ValueError with an HTTPException(404, ...) whose RFC 9457-style {"reason": "not_found", "message": ...} detail matches the other not-found responses in this route, so Starlette's HTTPException handler renders a clean 404.

This is a defensive branch (a task instance's run_id is FK-bound to a DagRun, so dr is None should not occur in a consistent DB), so it is a low-blast-radius error-path correctness fix with a regression test.

closes: #69392


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.8)

Generated-by: Claude Code (Opus 4.8) following the guidelines

A missing DagRun in the Execution API ti_run route raised a bare
ValueError, which escaped the route's DataError/SQLAlchemyError guards and
surfaced through the app-level catch-all as an opaque 500. A missing
resource should be reported to the caller as a 404, consistent with the
other not-found paths in this route.
@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:task-sdk labels Sep 10, 2026
@pierrejeambrun
pierrejeambrun marked this pull request as ready for review September 10, 2026 14:47

@henry3260 henry3260 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@kaxil kaxil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor comments, lgtm otherwise

Patching Session.scalars as a decorator also mocked it while
create_task_instance built the fixture, so the DAG bulk-write only worked
because the tables were cleared first. Scope the patch to the request
itself, matching the sibling database-error test.
@vatsrahul1001 vatsrahul1001 added the type:bug-fix Changelog: Bug Fixes label Sep 10, 2026
@pierrejeambrun
pierrejeambrun merged commit a9f6c89 into apache:main Sep 10, 2026
79 checks passed
@pierrejeambrun
pierrejeambrun deleted the fix-ti-run-missing-dagrun-404 branch September 10, 2026 17:16
@github-actions

Copy link
Copy Markdown
Contributor

Backport successfully created: v3-3-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
✅ v3-3-test PR Link

aws-airflow-bot pushed a commit to aws-mwaa/upstream-to-airflow that referenced this pull request Sep 10, 2026
…Run (apache#72890)

* Return 404 instead of 500 when ti_run cannot find the DagRun

A missing DagRun in the Execution API ti_run route raised a bare
ValueError, which escaped the route's DataError/SQLAlchemyError guards and
surfaced through the app-level catch-all as an opaque 500. A missing
resource should be reported to the caller as a 404, consistent with the
other not-found paths in this route.

* Update airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py

Co-authored-by: Henry Chen <[email protected]>

* Align test with the updated missing-DagRun error message

* Keep the ti_run 404 test's scalars mock out of fixture setup

Patching Session.scalars as a decorator also mocked it while
create_task_instance built the fixture, so the DAG bulk-write only worked
because the tables were cleared first. Scope the patch to the request
itself, matching the sibling database-error test.

---------
(cherry picked from commit a9f6c89)

Co-authored-by: Pierre Jeambrun <[email protected]>
Co-authored-by: Henry Chen <[email protected]>
vatsrahul1001 added a commit that referenced this pull request Sep 11, 2026
…Run (#72890) (#72900)

* [v3-3-test] Return 404 instead of 500 when ti_run cannot find the DagRun (#72890)

* Return 404 instead of 500 when ti_run cannot find the DagRun

A missing DagRun in the Execution API ti_run route raised a bare
ValueError, which escaped the route's DataError/SQLAlchemyError guards and
surfaced through the app-level catch-all as an opaque 500. A missing
resource should be reported to the caller as a 404, consistent with the
other not-found paths in this route.

* Update airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py

Co-authored-by: Henry Chen <[email protected]>

* Align test with the updated missing-DagRun error message

* Keep the ti_run 404 test's scalars mock out of fixture setup

Patching Session.scalars as a decorator also mocked it while
create_task_instance built the fixture, so the DAG bulk-write only worked
because the tables were cleared first. Scope the patch to the request
itself, matching the sibling database-error test.

---------
(cherry picked from commit a9f6c89)

Co-authored-by: Pierre Jeambrun <[email protected]>
Co-authored-by: Henry Chen <[email protected]>

* Define RUN_PAYLOAD on TestTIRunState so the ti_run 404 test can build its request

---------

Co-authored-by: Pierre Jeambrun <[email protected]>
Co-authored-by: Henry Chen <[email protected]>
Co-authored-by: Rahul Vats <[email protected]>
Co-authored-by: Rahul Vats <[email protected]>
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 area:task-sdk type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Execution API leaks unhandled ValueError as 500 Internal Server Error when DagRun is missing in ti_run route

5 participants