Skip to content

Fix EMR container job not cancelled on deferral timeout#64770

Open
aurangzaib048 wants to merge 12 commits intoapache:mainfrom
aurangzaib048:fix/emr-container-trigger-cleanup
Open

Fix EMR container job not cancelled on deferral timeout#64770
aurangzaib048 wants to merge 12 commits intoapache:mainfrom
aurangzaib048:fix/emr-container-trigger-cleanup

Conversation

@aurangzaib048
Copy link
Copy Markdown

@aurangzaib048 aurangzaib048 commented Apr 6, 2026

When EmrContainerOperator runs in deferrable mode and the trigger times out or the task is killed, the EMR job keeps running on the cluster. This leads to orphaned jobs consuming resources and duplicate executions on retry.

This PR adds cancel-on-kill support to EmrContainerTrigger following the same proven pattern as EMrServerlessStartJobTrigger (PR #51883):

  • Override run() in EmrContainerTrigger to catch asyncio.CancelledError and cancel the EMR job via hook.stop_query() when safe to do so
  • Add safe_to_cancel() check to distinguish user-initiated kills from triggerer restarts (avoids cancelling jobs during triggerer restart)
  • Add cancel_on_kill parameter (default True) for opt-out
  • Update EmrContainerOperator.execute_complete() to cancel the job when the trigger reports a failure/timeout event
  • All cancellation paths are wrapped in try/except to ensure proper error propagation (CancelledError is always re-raised, original AirflowException is preserved)

closes: #60517


Was generative AI tooling used to co-author this PR?
  • Yes

- Wrap stop_query() in try/except inside CancelledError handler to
  ensure CancelledError is always re-raised even if cancellation fails
- Wrap safe_to_cancel() call to prevent DB/API errors from replacing
  the CancelledError
- Wrap stop_query() in execute_complete() to preserve original error
- Narrow exception handler in run() from Exception to AirflowException
  to match base class behavior and avoid swallowing programming bugs
- Narrow except in get_task_state() to (KeyError, TypeError) with
  proper exception chaining via 'from e'
- Use "error" status in failure TriggerEvent to match base class
  convention and include return_key/return_value
@aurangzaib048 aurangzaib048 requested a review from o-nikolas as a code owner April 6, 2026 15:47
@boring-cyborg
Copy link
Copy Markdown

boring-cyborg bot commented Apr 6, 2026

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@boring-cyborg boring-cyborg bot added area:providers provider:amazon AWS/Amazon - related issues labels Apr 6, 2026
The base class hook() method is typed to return AwsGenericHook[Any],
which does not expose the stop_query() method specific to
EmrContainerHook. Add an explicit type annotation with a type ignore
to satisfy mypy.
@kaxil kaxil requested a review from Copilot April 10, 2026 19:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes orphaned EMR on EKS container jobs when deferrable tasks time out or are killed by adding cancel-on-cancel semantics to the trigger and ensuring the operator cancels the job on terminal error events.

Changes:

  • Add cancel_on_kill support to EmrContainerTrigger, including a safe_to_cancel() check and explicit CancelledError handling to stop the remote job.
  • Update EmrContainerOperator.execute_complete() to stop the EMR job when the trigger reports failure/timeout.
  • Add/extend unit tests to cover trigger serialization and cancellation behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
providers/amazon/src/airflow/providers/amazon/aws/triggers/emr.py Adds cancel-on-kill behavior and safe cancellation checks in EmrContainerTrigger.
providers/amazon/src/airflow/providers/amazon/aws/operators/emr.py Cancels EMR container jobs in execute_complete() on non-success events; passes cancel_on_kill=True to trigger.
providers/amazon/tests/unit/amazon/aws/triggers/test_emr.py Adds serialization and cancellation-path unit tests for EmrContainerTrigger.
providers/amazon/tests/unit/amazon/aws/operators/test_emr_containers.py Adds unit tests verifying job cancellation on execute_complete() failure.

- Add except Exception fallback in trigger run() to match base class
  behavior for non-AirflowException errors
- Wrap sync get_task_instance() and stop_query() with sync_to_async to
  avoid blocking the triggerer event loop
- Add cancel_on_kill parameter to EmrContainerOperator for user opt-out
- Add test verifying original error is preserved when stop_query fails
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

…omplete

- Pass virtual_cluster_id to EmrContainerHook in hook() so stop_query()
  can call cancel_job_run with the correct cluster ID
- Use validated_event.get("job_id") in execute_complete() as primary
  source since self.job_id may be None after deferral reconstruction
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

- Move 'from sqlalchemy import select' to module-level conditional
  block to avoid repeated function-scope imports
- Add spec=EmrContainerHook to MagicMock instances in trigger tests
  to catch attribute/typo bugs
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

- Merge except AirflowException and except Exception into single
  except Exception handler since both yield identical error events
- Remove test_serialization_includes_cancel_on_kill since the existing
  test_serialization already covers cancel_on_kill=True default
- Remove unused AirflowException import from triggers/emr.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

@eladkal eladkal requested a review from vincbeck April 11, 2026 13:27
The execution API uses task_id as key for non-mapped tasks but
"{task_id}_{map_index}" for mapped tasks (map_index >= 0). Use the
correct key format so safe_to_cancel works for mapped deferrable
EMR container jobs.
Copy link
Copy Markdown
Contributor

@vincbeck vincbeck left a comment

Choose a reason for hiding this comment

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

I understand the goal here but I am also wondering if it is worth it. You're adding a lot of code just for that use case, which creates significant complexity in the operator. By any chance, is there any way to make it simpler?

The worker running execute_complete may differ from the one that ran
execute, so self.job_id cannot be relied upon after deferral.
if job_id:
self.log.info("Cancelling EMR container job %s", job_id)
try:
self.hook.stop_query(job_id)
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.

You seem to cancel the job regardless of cancel_on_kill value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Triggerer timeout exception is not handled properly

3 participants