You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "victorymakes (via GitHub)" <gi...@apache.org> on 2026/07/06 07:16:56 UTC
[PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
victorymakes opened a new pull request, #69442:
URL: https://github.com/apache/airflow/pull/69442
## Summary
`DatabricksSubmitRunOperator` and `DatabricksRunNowOperator` both implement `on_kill()` to cancel the Databricks run when an Airflow task is killed (SIGTERM or `execution_timeout`). `DatabricksTaskBaseOperator` — the base for `DatabricksTaskOperator` and `DatabricksNotebookOperator` — is missing the same implementation, so Databricks jobs continue running after the Airflow task is killed, orphaning compute resources and incurring unnecessary cloud spend.
`DatabricksWorkflowTaskGroup` received `on_kill()` in #42115; this PR closes the remaining gap for standalone task operators.
## Changes
- Adds `on_kill()` to `DatabricksTaskBaseOperator` using `self.databricks_run_id`, which is:
- initialised to `None` in `__init__` (no `AttributeError` risk)
- set by `_launch_job()` the moment the run is submitted — earlier than any polling or permission calls
- Adds unit tests covering both the cancel and no-op paths
## Testing
```
pytest providers/databricks/tests/unit/databricks/operators/test_databricks.py -k "on_kill"
```
##### Was generative AI tooling used to co-author this PR?
- [x] Yes — Claude Code (Sonnet 4.6)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "Vamsi-klu (via GitHub)" <gi...@apache.org>.
Vamsi-klu commented on code in PR #69442:
URL: https://github.com/apache/airflow/pull/69442#discussion_r3635909791
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -1941,6 +1940,25 @@ def execute_complete(self, context: dict | None, event: dict) -> None:
errors = event.get("errors", [])
self._handle_terminal_run_state(run_state, errors)
+ def on_kill(self) -> None:
+ if self.databricks_run_id is None:
+ return
+ if self._databricks_workflow_task_group:
+ # Workflow member: cancel only this task's child run, not the shared parent workflow run.
+ # Cancelling the parent would also stop all sibling tasks.
+ try:
+ run_id_to_cancel = self._get_current_databricks_task()["run_id"]
+ except Exception:
Review Comment:
Thanks for routing workflow members through `_get_current_databricks_task()["run_id"]` - that matches `monitor_databricks_job`.
This fallback undoes that guarantee though. On any failure resolving the child run (API blip, missing task_key, kill racing shutdown), we cancel `self.databricks_run_id`, which for workflow members is the shared parent run and stops siblings - the bug this path is meant to avoid. `on_kill` is also one of the more failure-prone moments to make a fresh API call.
Prefer: log the exception and return without calling `cancel_run`, rather than falling back to the parent. A unit test where `_get_current_databricks_task` raises and `cancel_run` is not called with the parent id would lock that in.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "vio-ao (via GitHub)" <gi...@apache.org>.
vio-ao commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5054729802
Thanks for the review!
You're right — `self.databricks_run_id` is the shared parent workflow run ID for workflow members, so cancelling it would stop sibling tasks. Fixed in the latest commit.
`on_kill()` now mirrors `monitor_databricks_job`: for workflow members it calls `_get_current_databricks_task()["run_id"]` to get the child task's own run ID, while standalone operators continue to cancel via `self.databricks_run_id` directly.
Also added the regression test you suggested: parent run_id=1, `_get_current_databricks_task` returns child run_id=999, asserts `cancel_run(999)`.
The branch has also been squashed to a single commit to fix the `fix:` subject check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "Vamsi-klu (via GitHub)" <gi...@apache.org>.
Vamsi-klu commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5055103660
Thanks for the quick turnaround. I re-checked HEAD (`feb6783`) end-to-end.
### Confirmed fixed
- Workflow members cancel via `_get_current_databricks_task()["run_id"]` only (not the shared parent run id)
- On child-run resolve failure: log + return (no parent fallback)
- Unit coverage:
- standalone cancel
- no-op when `databricks_run_id` is `None`
- workflow member child cancel (`parent=1` -> `cancel_run(999)`)
- workflow member exception path (`cancel_run` not called)
- Single commit; subject no longer uses a rejected `fix:` prefix
### Residual before this is ready
Both touched files drop the first two lines of the standard ASF license header:
```text
#
# Licensed to the Apache Software Foundation (ASF) under one
```
Please restore the full header in:
1. `providers/databricks/src/airflow/providers/databricks/operators/databricks.py`
2. `providers/databricks/tests/unit/databricks/operators/test_databricks.py`
### Testing bar
No live Databricks credentials/UI testing needed for merge. This matches how `DatabricksSubmitRunOperator` / `DatabricksRunNowOperator` `on_kill` is validated: mock `cancel_run` and assert the correct run id.
A rebase onto current `main` will still be needed before merge (branch is quite behind). That can land together with the header fix.
Logic otherwise looks good from my side.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "vio-ao (via GitHub)" <gi...@apache.org>.
vio-ao commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5054723444
Thanks for the review!
You're right — `self.databricks_run_id` is the shared parent workflow run ID for workflow members, so cancelling it would stop sibling tasks. Fixed in the latest commit.
`on_kill()` now mirrors `monitor_databricks_job`: for workflow members it calls `_get_current_databricks_task()["run_id"]` to get the child task's own run ID, while standalone operators continue to cancel via `self.databricks_run_id` directly.
Also added the regression test you suggested: parent run_id=1, `_get_current_databricks_task` returns child run_id=999, asserts `cancel_run(999)`.
The branch has also been squashed to a single commit to fix the `fix:` subject check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal merged PR #69442:
URL: https://github.com/apache/airflow/pull/69442
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5088921492
cc @moomindani for Databricks team review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "victorymakes (via GitHub)" <gi...@apache.org>.
victorymakes commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5054732978
Thanks for the review!
You're right — `self.databricks_run_id` is the shared parent workflow run ID for workflow members, so cancelling it would stop sibling tasks. Fixed in the latest commit.
`on_kill()` now mirrors `monitor_databricks_job`: for workflow members it calls `_get_current_databricks_task()["run_id"]` to get the child task's own run ID, while standalone operators continue to cancel via `self.databricks_run_id` directly.
Also added the regression test you suggested: parent run_id=1, `_get_current_databricks_task` returns child run_id=999, asserts `cancel_run(999)`.
The branch has also been squashed to a single commit to fix the `fix:` subject check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "victorymakes (via GitHub)" <gi...@apache.org>.
victorymakes commented on code in PR #69442:
URL: https://github.com/apache/airflow/pull/69442#discussion_r3635929061
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -1941,6 +1940,25 @@ def execute_complete(self, context: dict | None, event: dict) -> None:
errors = event.get("errors", [])
self._handle_terminal_run_state(run_state, errors)
+ def on_kill(self) -> None:
+ if self.databricks_run_id is None:
+ return
+ if self._databricks_workflow_task_group:
+ # Workflow member: cancel only this task's child run, not the shared parent workflow run.
+ # Cancelling the parent would also stop all sibling tasks.
+ try:
+ run_id_to_cancel = self._get_current_databricks_task()["run_id"]
+ except Exception:
Review Comment:
Good catch. Removed the fallback — if `_get_current_databricks_task` raises, `on_kill` now logs the exception and returns without calling `cancel_run` at all. Added a test where the method raises and asserts `cancel_run` is not called with the parent id.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "victorymakes (via GitHub)" <gi...@apache.org>.
victorymakes commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5055156672
Rebased onto current main and fixed the missing license header. Branch is now a single commit on top of `84e520a`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add on_kill() to DatabricksTaskBaseOperator to cancel runs on task kill [airflow]
Posted by "Vamsi-klu (via GitHub)" <gi...@apache.org>.
Vamsi-klu commented on PR #69442:
URL: https://github.com/apache/airflow/pull/69442#issuecomment-5014062867
`databricks_run_id` is the shared parent workflow-run ID for a member of `DatabricksWorkflowTaskGroup`. Cancelling it here would therefore stop sibling tasks as well.
Could `on_kill` mirror `monitor_databricks_job`: use `_get_current_databricks_task()["run_id"]` for workflow members, while retaining `self.databricks_run_id` for standalone operators? A regression test should set parent run ID `1`, return child attempt ID `999`, and assert `cancel_run(999)`.
The branch also contains two `fix:` commit subjects, which current Airflow commit checks reject; those will need rewriting when the branch is rebased.
---
Drafted-by: Codex (GPT-5) (no human review before posting)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org