Skip to content

Fix task instance tries API returning duplicate entries for same task instance #50597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions airflow-core/src/airflow/models/taskinstancehistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ class TaskInstanceHistory(Base):

dag_run = relationship(
"DagRun",
primaryjoin="TaskInstanceHistory.run_id == DagRun.run_id",
primaryjoin="and_(TaskInstanceHistory.run_id == DagRun.run_id, DagRun.dag_id == TaskInstanceHistory.dag_id)",
viewonly=True,
foreign_keys=[run_id],
foreign_keys=[run_id, dag_id],
)

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,28 @@ def test_should_respond_200_with_versions(self, test_client, run_id, expected_ve
},
}

def test_should_not_return_duplicate_runs(self, test_client, session):
"""
Test that ensures the task instances query doesn't return duplicates due to the updated join/filter logic.
"""
self.create_task_instances(session, task_instances=[{"state": State.SUCCESS}], with_ti_history=True)
self.create_task_instances(
session,
dag_id="example_bash_operator",
task_instances=[{"state": State.SUCCESS}],
with_ti_history=True,
)

response = test_client.get(
"/dags/example_python_operator/dagRuns/TEST_DAG_RUN_ID/taskInstances/print_the_context/tries"
)

assert response.status_code == 200

response = response.json()

assert response["total_entries"] == 2


class TestPostClearTaskInstances(TestTaskInstanceEndpoint):
@pytest.mark.parametrize(
Expand Down