From 1e02ed1caaaf8c3494f92aa2734096b34b21de12 Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Tue, 27 Oct 2026 17:40:31 +0530 Subject: [PATCH 1/5] fix(airflow): stop an active run when deleting its DAG Airflow's delete_dag refuses while a task instance is running. The delete operation returned a 500 that OpenMetadata ignores, and OpenMetadata removes the ingestion pipeline before it calls Airflow, so the pipeline disappeared while its DAG stayed in Airflow and kept running on its schedule. The delete is a hard delete the user already confirmed, so when Airflow refuses, fail the DAG's unfinished runs and task instances the way kill does and delete again, in the same session. The kill operation now shares that step. --- .../operations/delete.py | 29 +++++++---- .../operations/kill_all.py | 51 ++++++++++++------- .../operations/test_dag_lifecycle.py | 21 ++++---- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py index 452b4a2b48dc..7386e8cfea4d 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py @@ -16,7 +16,7 @@ from pathlib import Path from airflow import settings -from airflow.exceptions import DagNotFound +from airflow.exceptions import AirflowException, DagNotFound from flask import Response try: @@ -29,6 +29,7 @@ DAG_GENERATED_CONFIGS, ) from openmetadata_managed_apis.api.response import ApiResponse +from openmetadata_managed_apis.operations.kill_all import fail_unfinished_runs from openmetadata_managed_apis.utils.logger import operations_logger logger = operations_logger() @@ -43,11 +44,9 @@ def delete_dag_id(dag_id: str) -> Response: - DAG metadata, versions, runs and task history in airflow db Order matters: run Airflow's own delete FIRST so that its FK ordering - (a task instance pins the DAG version it ran, so deleting the DAG first - is refused when a task is still running) is enforced BEFORE the files - are removed. When Airflow refuses (raises AirflowException / RuntimeError - on running task instances), the files stay on disk and Airflow's next - parse can restore the DAG rather than leaving an orphan. + (a task instance pins the DAG version it ran) is enforced BEFORE the + files are removed. If that delete still fails, the files stay on disk + and Airflow's next parse can restore the DAG rather than leaving an orphan. :param dag_id: DAG to delete :return: API Response """ @@ -56,10 +55,7 @@ def delete_dag_id(dag_id: str) -> Response: config_file = Path(DAG_GENERATED_CONFIGS) / f"{dag_id}.json" with settings.Session() as session: - try: - deleted_dags = airflow_delete_dag(dag_id, session=session) - except DagNotFound: - deleted_dags = 0 + deleted_dags = _delete_dag_records(dag_id, session) session.commit() deleted_file = False @@ -83,3 +79,16 @@ def delete_dag_id(dag_id: str) -> Response: deleted_config, ) return ApiResponse.server_error() + + +def _delete_dag_records(dag_id: str, session) -> int: + try: + return airflow_delete_dag(dag_id, session=session) + except DagNotFound: + return 0 + except AirflowException: + # Airflow refuses while a task is running (e.g. a scheduled run); the caller is deleting + # the pipeline for good, so stop the run the same way kill does and delete again + logger.info("Stopping the active run of DAG %s before deleting it", dag_id) + fail_unfinished_runs(dag_id, session) + return airflow_delete_dag(dag_id, session=session) diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py index 78106a3d6288..2289b5311679 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py @@ -34,29 +34,42 @@ def kill_all(dag_id: str) -> Response: if not dag_model: return ApiResponse.not_found(f"DAG {dag_id} not found.") - runs: list[DagRun] = ( - session.query(DagRun) - .filter( - DagRun.dag_id == dag_id, - DagRun.state.in_((DagRunState.RUNNING, DagRunState.QUEUED)), - ) - .all() - ) + if not fail_unfinished_runs(dag_id, session): + return ApiResponse.not_found(f"Workflow [{dag_id}] has no running or pending runs nor tasks") + + session.commit() + + return ApiResponse.success({"message": f"Workflow [{dag_id}] has been killed"}) - instances: list[TaskInstance] = session.query(TaskInstance).filter( - TaskInstance.dag_id == dag_id, - TaskInstance.state.notin_((TaskInstanceState.SUCCESS, TaskInstanceState.FAILED)), + +def fail_unfinished_runs(dag_id: str, session) -> bool: + """ + Mark the DAG's running or queued runs and their unfinished task instances as failed. + :param dag_id: DAG whose runs to stop + :param session: Airflow DB session; the caller commits + :return: whether there was anything to stop + """ + runs: list[DagRun] = ( + session.query(DagRun) + .filter( + DagRun.dag_id == dag_id, + DagRun.state.in_((DagRunState.RUNNING, DagRunState.QUEUED)), ) + .all() + ) - if not runs or not instances: - return ApiResponse.not_found(f"Workflow [{dag_id}] has no running or pending runs nor tasks") + instances: list[TaskInstance] = session.query(TaskInstance).filter( + TaskInstance.dag_id == dag_id, + TaskInstance.state.notin_((TaskInstanceState.SUCCESS, TaskInstanceState.FAILED)), + ) - for dag_run in runs: - dag_run.set_state(DagRunState.FAILED) + if not runs or not instances: + return False - for instance in instances: - instance.set_state(TaskInstanceState.FAILED) + for dag_run in runs: + dag_run.set_state(DagRunState.FAILED) - session.commit() + for instance in instances: + instance.set_state(TaskInstanceState.FAILED) - return ApiResponse.success({"message": f"Workflow [{dag_id}] has been killed"}) + return True diff --git a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py index aeda1485f985..306bf4f0e136 100644 --- a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py +++ b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py @@ -22,7 +22,6 @@ import pytest from airflow import DAG, settings from airflow.api.common.delete_dag import delete_dag as airflow_delete_dag -from airflow.exceptions import AirflowException from airflow.models import DagModel, DagRun, TaskInstance from airflow.utils import timezone from airflow.utils.state import DagRunState, TaskInstanceState @@ -102,20 +101,22 @@ def test_delete_dag_with_versioned_task_history(stored_dag): assert session.query(model).filter_by(dag_id=name).count() == 0 -def test_running_dag_keeps_its_files_when_deletion_is_rejected(stored_dag): - name, dag_file, config_file, _ = stored_dag +def test_delete_stops_a_running_dag_and_removes_it(stored_dag): + name, dag_file, config_file, version_model = stored_dag with settings.Session() as session: + session.query(DagRun).filter_by(dag_id=name).update({"state": DagRunState.RUNNING}) session.query(TaskInstance).filter_by(dag_id=name).update({"state": TaskInstanceState.RUNNING}) session.commit() - with ( - Flask(__name__).app_context(), - pytest.raises(AirflowException, match="TaskInstances still running"), - ): - delete.delete_dag_id(name) + with Flask(__name__).app_context(): + response = delete.delete_dag_id(name) - assert dag_file.exists() - assert config_file.exists() + assert response.status_code == 200 + assert not dag_file.exists() + assert not config_file.exists() + with settings.Session() as session: + for model in (TaskInstance, DagRun, version_model, DagModel): + assert session.query(model).filter_by(dag_id=name).count() == 0 def test_invalid_dag_does_not_report_success(tmp_path, monkeypatch): From 48d97eadc55a279fca01eb518aa5ca5b72c91aee Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:03:13 +0530 Subject: [PATCH 2/5] fix(airflow): stop runs only for Airflow's running-task refusal, and any running task Review follow-up: the delete fallback now stops runs only when Airflow refused because a task instance is running (other AirflowExceptions are re-raised untouched), and it fails unfinished task instances even when their run is no longer running. Kill keeps its behaviour; the never-true 'not instances' guard on a Query is dropped. --- .../operations/delete.py | 17 +++++++++- .../operations/kill_all.py | 31 +++++++++++++------ .../operations/test_dag_lifecycle.py | 26 ++++++++++++++-- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py index 7386e8cfea4d..8b8a0eaffe89 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py @@ -17,6 +17,8 @@ from airflow import settings from airflow.exceptions import AirflowException, DagNotFound +from airflow.models import TaskInstance +from airflow.utils.state import TaskInstanceState from flask import Response try: @@ -29,7 +31,10 @@ DAG_GENERATED_CONFIGS, ) from openmetadata_managed_apis.api.response import ApiResponse -from openmetadata_managed_apis.operations.kill_all import fail_unfinished_runs +from openmetadata_managed_apis.operations.kill_all import ( + fail_unfinished_runs, + fail_unfinished_task_instances, +) from openmetadata_managed_apis.utils.logger import operations_logger logger = operations_logger() @@ -89,6 +94,16 @@ def _delete_dag_records(dag_id: str, session) -> int: except AirflowException: # Airflow refuses while a task is running (e.g. a scheduled run); the caller is deleting # the pipeline for good, so stop the run the same way kill does and delete again + if not _has_running_task_instances(dag_id, session): + raise logger.info("Stopping the active run of DAG %s before deleting it", dag_id) fail_unfinished_runs(dag_id, session) + fail_unfinished_task_instances(dag_id, session) return airflow_delete_dag(dag_id, session=session) + + +def _has_running_task_instances(dag_id: str, session) -> bool: + running = session.query(TaskInstance).filter( + TaskInstance.dag_id == dag_id, TaskInstance.state == TaskInstanceState.RUNNING + ) + return session.query(running.exists()).scalar() diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py index 2289b5311679..0482c69fc313 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py @@ -47,7 +47,7 @@ def fail_unfinished_runs(dag_id: str, session) -> bool: Mark the DAG's running or queued runs and their unfinished task instances as failed. :param dag_id: DAG whose runs to stop :param session: Airflow DB session; the caller commits - :return: whether there was anything to stop + :return: whether there was a run to stop """ runs: list[DagRun] = ( session.query(DagRun) @@ -58,18 +58,31 @@ def fail_unfinished_runs(dag_id: str, session) -> bool: .all() ) - instances: list[TaskInstance] = session.query(TaskInstance).filter( - TaskInstance.dag_id == dag_id, - TaskInstance.state.notin_((TaskInstanceState.SUCCESS, TaskInstanceState.FAILED)), - ) - - if not runs or not instances: + if not runs: return False for dag_run in runs: dag_run.set_state(DagRunState.FAILED) - for instance in instances: - instance.set_state(TaskInstanceState.FAILED) + fail_unfinished_task_instances(dag_id, session) return True + + +def fail_unfinished_task_instances(dag_id: str, session) -> None: + """ + Mark every unfinished task instance of the DAG as failed, whatever the state of its run. + :param dag_id: DAG whose task instances to stop + :param session: Airflow DB session; the caller commits + """ + instances: list[TaskInstance] = ( + session.query(TaskInstance) + .filter( + TaskInstance.dag_id == dag_id, + TaskInstance.state.notin_((TaskInstanceState.SUCCESS, TaskInstanceState.FAILED)), + ) + .all() + ) + + for instance in instances: + instance.set_state(TaskInstanceState.FAILED) diff --git a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py index 306bf4f0e136..7f8618bfa690 100644 --- a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py +++ b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py @@ -22,6 +22,7 @@ import pytest from airflow import DAG, settings from airflow.api.common.delete_dag import delete_dag as airflow_delete_dag +from airflow.exceptions import AirflowException from airflow.models import DagModel, DagRun, TaskInstance from airflow.utils import timezone from airflow.utils.state import DagRunState, TaskInstanceState @@ -101,10 +102,11 @@ def test_delete_dag_with_versioned_task_history(stored_dag): assert session.query(model).filter_by(dag_id=name).count() == 0 -def test_delete_stops_a_running_dag_and_removes_it(stored_dag): +@pytest.mark.parametrize("run_state", [DagRunState.RUNNING, DagRunState.SUCCESS]) +def test_delete_stops_a_running_dag_and_removes_it(stored_dag, run_state): name, dag_file, config_file, version_model = stored_dag with settings.Session() as session: - session.query(DagRun).filter_by(dag_id=name).update({"state": DagRunState.RUNNING}) + session.query(DagRun).filter_by(dag_id=name).update({"state": run_state}) session.query(TaskInstance).filter_by(dag_id=name).update({"state": TaskInstanceState.RUNNING}) session.commit() @@ -119,6 +121,26 @@ def test_delete_stops_a_running_dag_and_removes_it(stored_dag): assert session.query(model).filter_by(dag_id=name).count() == 0 +def test_other_airflow_errors_do_not_stop_active_runs(stored_dag, monkeypatch): + name, dag_file, _, _ = stored_dag + with settings.Session() as session: + session.query(DagRun).filter_by(dag_id=name).update({"state": DagRunState.RUNNING}) + session.query(TaskInstance).filter_by(dag_id=name).update({"state": TaskInstanceState.QUEUED}) + session.commit() + + def refuse(dag_id, session=None): + raise AirflowException("unrelated failure") + + monkeypatch.setattr(delete, "airflow_delete_dag", refuse) + with Flask(__name__).app_context(), pytest.raises(AirflowException, match="unrelated failure"): + delete.delete_dag_id(name) + + assert dag_file.exists() + with settings.Session() as session: + assert session.query(DagRun).filter_by(dag_id=name).one().state == DagRunState.RUNNING + assert session.query(TaskInstance).filter_by(dag_id=name).one().state == TaskInstanceState.QUEUED + + def test_invalid_dag_does_not_report_success(tmp_path, monkeypatch): path = tmp_path / "invalid.py" path.write_text('from airflow import DAG\nraise ValueError("invalid ingestion config")\n') From 8219fbb388af43822ea46773e48cb9cbda529679 Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:41:15 +0530 Subject: [PATCH 3/5] docs(decisions): record that deleting a pipeline DAG stops its active run --- ...ing-a-pipeline-dag-stops-its-active-run.md | 28 +++++++++++++++++++ .../operations/delete.py | 1 + 2 files changed, 29 insertions(+) create mode 100644 docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md diff --git a/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md b/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md new file mode 100644 index 000000000000..fbd52b4576e7 --- /dev/null +++ b/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md @@ -0,0 +1,28 @@ +# Deleting a pipeline's Airflow DAG stops its active run instead of failing + +- **Status:** Accepted +- **Revisions:** v1 2026-10-29 (initial) +- **Deciders:** Sonika Shah +- **Guard:** `test_delete_stops_a_running_dag_and_removes_it`, `test_other_airflow_errors_do_not_stop_active_runs` in `openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py` +- **Related:** #36350, #36349, #36267 + +## Context + +Airflow's `delete_dag` refuses while any task instance of the DAG is running. The server commits +the hard delete of an ingestion pipeline (`cleanup()`) before `postDelete` asks the runner to +remove the DAG, so a refusal cannot keep the pipeline: returning 409 from the plugin left the +pipeline deleted and the DAG in Airflow, still scheduled, with nothing in OpenMetadata to manage it. + +## Decision + +When `delete_dag` raises because a task instance is running, the plugin's delete operation fails +the DAG's unfinished runs and task instances (as `kill` does) and deletes again in the same +session. Any other Airflow error still propagates and leaves the runs and files untouched. + +## Consequences + +- Every delete path (direct, by name, async, force, service recursive cascade, app uninstall) + removes the DAG, because all of them reach this one plugin endpoint. +- Deleting a pipeline mid-run ends that run as failed; the running task is terminated by Airflow. +- Revisit only if the server moves the runner teardown ahead of its DB commit; then refusing the + delete would become a viable alternative. diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py index 8b8a0eaffe89..6be107fa0346 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py @@ -94,6 +94,7 @@ def _delete_dag_records(dag_id: str, session) -> int: except AirflowException: # Airflow refuses while a task is running (e.g. a scheduled run); the caller is deleting # the pipeline for good, so stop the run the same way kill does and delete again + # (ADR:2026-10-29-deleting-a-pipeline-dag-stops-its-active-run) if not _has_running_task_instances(dag_id, session): raise logger.info("Stopping the active run of DAG %s before deleting it", dag_id) From b0b00d9e78079c1d7ef7157ddaa1008e042f8c18 Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Thu, 29 Oct 2026 01:05:52 +0530 Subject: [PATCH 4/5] fix(airflow): keep the delete retry's run stop in the caller's transaction --- ...29-deleting-a-pipeline-dag-stops-its-active-run.md | 9 ++++++--- .../openmetadata_managed_apis/operations/delete.py | 4 ++-- .../openmetadata_managed_apis/operations/kill_all.py | 2 +- .../integration/operations/test_dag_lifecycle.py | 11 ++++++----- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md b/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md index fbd52b4576e7..7df56b2cdf1b 100644 --- a/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md +++ b/docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md @@ -3,7 +3,7 @@ - **Status:** Accepted - **Revisions:** v1 2026-10-29 (initial) - **Deciders:** Sonika Shah -- **Guard:** `test_delete_stops_a_running_dag_and_removes_it`, `test_other_airflow_errors_do_not_stop_active_runs` in `openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py` +- **Guard:** `test_delete_stops_a_running_dag_and_removes_it`, `test_failed_delete_leaves_active_runs_untouched` in `openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py` - **Related:** #36350, #36349, #36267 ## Context @@ -16,13 +16,16 @@ pipeline deleted and the DAG in Airflow, still scheduled, with nothing in OpenMe ## Decision When `delete_dag` raises because a task instance is running, the plugin's delete operation fails -the DAG's unfinished runs and task instances (as `kill` does) and deletes again in the same -session. Any other Airflow error still propagates and leaves the runs and files untouched. +the DAG's unfinished runs and task instances (as `kill` does) and deletes again, in one +transaction: if the retried delete also fails, nothing is committed, so the runs stay as they were +and the DAG files stay on disk. Any other Airflow error propagates the same way. ## Consequences - Every delete path (direct, by name, async, force, service recursive cascade, app uninstall) removes the DAG, because all of them reach this one plugin endpoint. - Deleting a pipeline mid-run ends that run as failed; the running task is terminated by Airflow. +- A task that starts running between the stop and the retry makes the retry fail; the delete then + fails as it did before this record, with the run left untouched. - Revisit only if the server moves the runner teardown ahead of its DB commit; then refusing the delete would become a viable alternative. diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py index 6be107fa0346..5c13a56106ef 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py @@ -98,8 +98,8 @@ def _delete_dag_records(dag_id: str, session) -> int: if not _has_running_task_instances(dag_id, session): raise logger.info("Stopping the active run of DAG %s before deleting it", dag_id) - fail_unfinished_runs(dag_id, session) - fail_unfinished_task_instances(dag_id, session) + if not fail_unfinished_runs(dag_id, session): + fail_unfinished_task_instances(dag_id, session) return airflow_delete_dag(dag_id, session=session) diff --git a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py index 0482c69fc313..9d76f761cee5 100644 --- a/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py +++ b/openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py @@ -85,4 +85,4 @@ def fail_unfinished_task_instances(dag_id: str, session) -> None: ) for instance in instances: - instance.set_state(TaskInstanceState.FAILED) + instance.set_state(TaskInstanceState.FAILED, session=session) diff --git a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py index 7f8618bfa690..28b689f195fd 100644 --- a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py +++ b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py @@ -121,24 +121,25 @@ def test_delete_stops_a_running_dag_and_removes_it(stored_dag, run_state): assert session.query(model).filter_by(dag_id=name).count() == 0 -def test_other_airflow_errors_do_not_stop_active_runs(stored_dag, monkeypatch): +@pytest.mark.parametrize("ti_state", [TaskInstanceState.QUEUED, TaskInstanceState.RUNNING]) +def test_failed_delete_leaves_active_runs_untouched(stored_dag, monkeypatch, ti_state): name, dag_file, _, _ = stored_dag with settings.Session() as session: session.query(DagRun).filter_by(dag_id=name).update({"state": DagRunState.RUNNING}) - session.query(TaskInstance).filter_by(dag_id=name).update({"state": TaskInstanceState.QUEUED}) + session.query(TaskInstance).filter_by(dag_id=name).update({"state": ti_state}) session.commit() def refuse(dag_id, session=None): - raise AirflowException("unrelated failure") + raise AirflowException("refused") monkeypatch.setattr(delete, "airflow_delete_dag", refuse) - with Flask(__name__).app_context(), pytest.raises(AirflowException, match="unrelated failure"): + with Flask(__name__).app_context(), pytest.raises(AirflowException, match="refused"): delete.delete_dag_id(name) assert dag_file.exists() with settings.Session() as session: assert session.query(DagRun).filter_by(dag_id=name).one().state == DagRunState.RUNNING - assert session.query(TaskInstance).filter_by(dag_id=name).one().state == TaskInstanceState.QUEUED + assert session.query(TaskInstance).filter_by(dag_id=name).one().state == ti_state def test_invalid_dag_does_not_report_success(tmp_path, monkeypatch): From c3a83241324f38902e9de51400858c8ac4916acd Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Thu, 29 Oct 2026 01:16:09 +0530 Subject: [PATCH 5/5] test(airflow): assert a failed delete also keeps the generated config --- .../tests/integration/operations/test_dag_lifecycle.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py index 28b689f195fd..a85955ff203c 100644 --- a/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py +++ b/openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py @@ -123,7 +123,7 @@ def test_delete_stops_a_running_dag_and_removes_it(stored_dag, run_state): @pytest.mark.parametrize("ti_state", [TaskInstanceState.QUEUED, TaskInstanceState.RUNNING]) def test_failed_delete_leaves_active_runs_untouched(stored_dag, monkeypatch, ti_state): - name, dag_file, _, _ = stored_dag + name, dag_file, config_file, _ = stored_dag with settings.Session() as session: session.query(DagRun).filter_by(dag_id=name).update({"state": DagRunState.RUNNING}) session.query(TaskInstance).filter_by(dag_id=name).update({"state": ti_state}) @@ -137,6 +137,7 @@ def refuse(dag_id, session=None): delete.delete_dag_id(name) assert dag_file.exists() + assert config_file.exists() with settings.Session() as session: assert session.query(DagRun).filter_by(dag_id=name).one().state == DagRunState.RUNNING assert session.query(TaskInstance).filter_by(dag_id=name).one().state == ti_state