Skip to content

fix(airflow): stop an active run when deleting its DAG - #36349

Merged
Khairajani merged 5 commits into
mainfrom
fix/ingestion-delete-airflow-refusal
Oct 31, 2026
Merged

Khairajani merged 5 commits into
mainfrom
fix/ingestion-delete-airflow-refusal

Conversation

@sonika-shah

@sonika-shah sonika-shah commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #36350

Summary

Hard-deleting an ingestion pipeline while one of its tasks is running left the DAG in Airflow, still scheduled, with no pipeline in OpenMetadata.

Airflow's delete_dag refuses while a task instance is running. OpenMetadata removes the pipeline before it calls Airflow, and it ignores the 500 that comes back. Now, when Airflow refuses, the delete operation stops the active run (the same way kill does) and deletes again. The stop and the delete commit together, or roll back together: if the retried delete fails, the run is left as it was and the DAG files stay. The user already confirmed the delete, so the run should not outlive it.

Follow-up from the review of the 2.0 backport #36267.

What changed

  • operations/delete.py: when delete_dag refuses because a task is running, fail the DAG's unfinished runs and task instances, then delete. Any other Airflow error is raised as before.
  • operations/kill_all.py: the "fail unfinished runs" step moves into fail_unfinished_runs and fail_unfinished_task_instances, which kill and delete now share. set_state now gets the caller's session, so the stop is not committed on its own before the delete. Kill's behaviour is unchanged.
  • test_dag_lifecycle.py: the running-task test now expects the delete to succeed and remove everything. A new test checks that a failed delete leaves the run, task instance, DAG file and config untouched, with the task queued or running.
  • docs/decisions/2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md: records why delete stops the run instead of refusing.

No server change. Every delete path (direct, by name, async, force, service recursive, external app uninstall) goes through this one plugin endpoint.

Why not fail the delete with a 409 instead

I tried that first: the plugin returned 409, and OpenMetadata threw it from IngestionPipelineRepository.postDelete. On main, cleanup() commits the delete in its own transaction before postDelete calls the runner. So the user got a 409, but the pipeline was already gone and the DAG stayed. Keeping the pipeline would mean moving the runner teardown ahead of the DB delete for direct and cascade deletes, which is a much bigger change to the delete flow. Stopping the run fixes every path with a change inside the plugin only.

Testing

Plugin tests (Airflow 3.3.2, SQLite): tests/unit + tests/integration/operations, 70 passed, 1 skipped. The running-task delete test fails on main with TaskInstances still running; the failed-delete test with a running task fails without the session= fix (the run was already committed as failed). ruff check and ruff format --check are clean. test_airflow_ops.py and ingestion_pipeline/* need a live server and were not run here.

Full stack: OpenMetadata server built from main, Postgres, ES 9.3, Airflow 3.3.2 with this plugin.

Delete while a task is running (6/6):

Path Result
DELETE /ingestionPipelines/{id}?hardDelete=true (what the UI calls) 200; pipeline, DAG rows and files gone; still gone 45s later
DELETE /ingestionPipelines/name/{fqn}?hardDelete=true 200, all gone
DELETE /ingestionPipelines/async/{id}?hardDelete=true 202, all gone
...&force=true 200, all gone
DELETE /databaseServices/{id}?hardDelete=true&recursive=true service, pipeline, DAG all gone
POST /ingestionPipelines/kill/{id} on a running pipeline 200, run failed (kill still works after the refactor)

Regression (8/9, same as main):

  • Hard delete after a completed run; deployed-but-never-run; never deployed.
  • Soft delete → restore → rerun → hard delete.
  • Recreate a pipeline with the same name.
  • Force, by-name and async delete after a run.
  • Recursive service delete.
  • Deploy / trigger / logs / status / toggle / edit + redeploy.

The one failure is the harness calling kill after its short run had already finished: Airflow answers 400 "no running runs", the same on main without this change.

RetriggerView in GreptileConfidence Score: 5/5

The latest changes appear safe to merge; the earlier task-shutdown test gap remains non-blocking.

Findings

  1. P2 Task shutdown is not tested ▶

Summary

The PR retries DAG deletion after marking unfinished runs and tasks as failed.

  • Deleting a DAG stops its running task before trying again.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Delete DAG records] --> B{Airflow refuses?}
    B -->|No| F[Commit and remove files]
    B -->|Yes| C{Running task row exists?}
    C -->|No| D[Return the error and keep files]
    C -->|Yes| E[Mark unfinished runs and tasks failed]
    E --> G[Retry deletion in the same session]
    G --> F
Loading

Reviews (2) · Last reviewed commit: "fix(airflow): stop runs only for Airflow..." · Reviewed by Greptile

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.
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Oct 27, 2026
…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.
Copilot AI balanced review requested due to automatic review settings October 28, 2026 19:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the DAG deletion retry and rollback changes.

No open issues 🎉

What changed in this PR

Addresses #36350 in OpenMetadata’s Airflow plugin by retrying DAG deletion after marking unfinished runs and tasks as failed, preventing running-task refusals from leaving orphan scheduled DAGs.

Changes:

  • Shares run/task failure helpers between kill and delete, with deletion retried in the same transaction.
  • Adds regression coverage for deleting running tasks and preserving active state when deletion fails.
  • Records the deletion policy and rollback behavior in a decision record.
File Description
openmetadata-airflow-apis/​tests/​integration/​operations/​test_dag_lifecycle.py Tests successful running-task deletion and rollback on refusal.
openmetadata-airflow-apis/​openmetadata_managed_apis/​operations/​kill_all.py Extracts shared helpers for failing unfinished runs and tasks.
openmetadata-airflow-apis/​openmetadata_managed_apis/​operations/​delete.py Handles running-task refusals and retries deletion before removing files.
docs/​decisions/​2026-10-29-deleting-a-pipeline-dag-stops-its-active-run.md Documents the deletion policy, transaction guarantees, and consequences.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 28, 2026 19:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@gitar-bot

gitar-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 closed / 2 findings

🔴 High risk · Retrying DAG deletion after failing active runs removes Airflow records, potentially discarding in-progress execution state and task history.

Deleting an ingestion pipeline now stops its active Airflow run and retries the DAG delete in the same session, so the run no longer outlives the confirmed delete. Resolved: delete is still refused when a task instance is running without a running or queued DagRun, and the stop-active-run rule is now documented beyond the PR description. No open issues found.

✅ 2 closed
✅ Edge Case: Delete is still refused if a TI runs without a running/queued DagRun

📄 openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py:89-94 📄 openmetadata-airflow-apis/openmetadata_managed_apis/operations/kill_all.py:52-66 📄 openmetadata-airflow-apis/tests/integration/operations/test_dag_lifecycle.py:107
Airflow's delete_dag refuses whenever any TaskInstance of the DAG is RUNNING. The retry in _delete_dag_records (delete.py:89-94) only clears that state through fail_unfinished_runs, which returns early when no DagRun is RUNNING/QUEUED (kill_all.py:52-67). instances is an unevaluated Query, so it is always truthy and the guard reduces to not runs. So if a task instance is still RUNNING after its run was already marked failed, success or timed out, nothing gets failed, the second airflow_delete_dag raises the same AirflowException, and the DAG is left behind in Airflow. This is the orphan this PR is meant to prevent. The updated test only passes because it now also forces the DagRun to RUNNING (test_dag_lifecycle.py:107). Fix: on the delete path, fail the unfinished task instances whatever state their run is in, and add a test that leaves the DagRun finished while a TI is RUNNING.

✅ Quality: Delete-stops-active-run rule is only in the PR description

📄 openmetadata-airflow-apis/openmetadata_managed_apis/operations/delete.py:84-94
This PR changes the delete contract between the server and the Airflow plugin. Before, a delete was refused while a task was running. Now the plugin fails the active run and deletes anyway. The reason is stated only in the description: "A 409 was not used because cleanup() commits the pipeline delete before postDelete calls the runner", and the fix "lives entirely in the plugin, so every delete path ... gets the same behaviour". The repo squash-merges, and no record in docs/decisions/ covers this. A later change could reasonably bring back a refusal or a 409 and recreate the orphaned-DAG bug. Add a record stating: "Deleting a pipeline's Airflow DAG never fails because a run is active: the plugin fails the DAG's unfinished runs and task instances and then deletes, because the server commits the pipeline delete before it calls the runner." Cite it from _delete_dag_records.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@sonarqubecloud

Copy link
Copy Markdown

@Khairajani
Khairajani added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚦 Removed from the merge queue — failed_checks (2026-10-29T21:30:15Z)

Blocked the queue: playwright-summary

@Khairajani
Khairajani added this pull request to the merge queue Oct 30, 2026
Merged via the queue into main with commit 4849e61 Oct 31, 2026
76 of 78 checks passed
@Khairajani
Khairajani deleted the fix/ingestion-delete-airflow-refusal branch October 31, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hard-deleting an ingestion pipeline while it runs leaves its DAG scheduled in Airflow

3 participants