Skip to content

Commit 90bb554

Browse files
committed
fixup! Read current task executions by exact region and try
The task instance clear endpoint now takes its run locks through the shared helper, so the clears it calls do not lock the same runs again. It keeps reloading the locked runs, since the run state it acts on may have changed while it waited for the lock. The conversion lives here rather than in the clear layer because this layer rewrote the endpoint's imports. Reading a loop task's execution without its pass must answer the same 400 as marking it, whether or not a region id is given; until the read routes took region coordinates there was no read to pin this on.
1 parent f3ab4ee commit 90bb554

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/public/task_instances.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import structlog
2626
from fastapi import Depends, HTTPException, Query, status
27-
from sqlalchemy import select, tuple_
27+
from sqlalchemy import select
2828
from sqlalchemy.orm import joinedload
2929

3030
from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity
@@ -129,6 +129,7 @@
129129
from airflow.api_fastapi.logging.decorators import action_logging
130130
from airflow.exceptions import AirflowClearRunningTaskException, TaskNotFound
131131
from airflow.models import DagRun
132+
from airflow.models.dagrun import lock_dag_runs
132133
from airflow.models.dynamic_region import SENTINEL_REGION_ID
133134
from airflow.models.renderedtifields import load_legacy_rendered_fields
134135
from airflow.models.task_coordinates import (
@@ -1148,13 +1149,7 @@ def select_candidates() -> Sequence[TI]:
11481149
if task_instances:
11491150
if not dry_run:
11501151
run_keys = {(ti.dag_id, ti.run_id) for ti in task_instances}
1151-
session.scalars(
1152-
select(DagRun)
1153-
.where(tuple_(DagRun.dag_id, DagRun.run_id).in_(run_keys))
1154-
.order_by(DagRun.dag_id, DagRun.run_id)
1155-
.with_for_update()
1156-
.execution_options(populate_existing=True)
1157-
).all()
1152+
lock_dag_runs(session, run_keys, refresh=True)
11581153
if body.task_instance_ids is None:
11591154
task_instances = [ti for ti in select_candidates() if (ti.dag_id, ti.run_id) in run_keys]
11601155
task_instances = session.scalars(

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_task_instances.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5795,6 +5795,20 @@ def test_loop_mark_without_coordinates_requires_a_region_and_index(self, test_cl
57955795
assert response.status_code == 400, response.text
57965796
assert response.json()["detail"] == "Select a region and index for this task instance"
57975797

5798+
@pytest.mark.parametrize("with_region_id", [False, True])
5799+
def test_loop_read_without_index_requires_a_region_and_index(
5800+
self, test_client, loop_instances, with_region_id
5801+
):
5802+
dr, loop, root, tis = loop_instances
5803+
5804+
response = test_client.get(
5805+
f"/dags/{dr.dag_id}/dagRuns/{dr.run_id}/taskInstances/body.first",
5806+
params={"region_id": str(root.id)} if with_region_id else {},
5807+
)
5808+
5809+
assert response.status_code == 400, response.text
5810+
assert response.json()["detail"] == "Select a region and index for this task instance"
5811+
57985812
@pytest.mark.parametrize(
57995813
("map_index", "expected_status"),
58005814
[

0 commit comments

Comments
 (0)