Skip to content

Fix EmrContainerSensor reporting success for an unknown job state - #72500

Merged
vincbeck merged 2 commits into
apache:mainfrom
Priyaj11:fix-emr-container-sensor-unknown-state
Sep 4, 2026
Merged

vincbeck merged 2 commits into
apache:mainfrom
Priyaj11:fix-emr-container-sensor-unknown-state

Conversation

@Priyaj11

@Priyaj11 Priyaj11 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

What happened

EmrContainerSensor.poke() treats "not a failure state and not an intermediate state" as success:

python
INTERMEDIATE_STATES = ("PENDING", "SUBMITTED", "RUNNING")
FAILURE_STATES = ("FAILED", "CANCELLED", "CANCEL_PENDING")
SUCCESS_STATES = ("COMPLETED",) # declared, never used

def poke(self, context: Context) -> bool:
state = self.hook.poll_query_status(...)
if state in self.FAILURE_STATES:
raise AirflowException(...)
if state in self.INTERMEDIATE_STATES:
return False
return True # anything else is "success"

SUCCESS_STATES is defined on the class and never referenced anywhere in the file.

Why it matters

The fall-through is reachable through the hook's own documented behaviour:

EmrContainerHook.check_query_status catches a generic ClientError, logs it and returns None. Its docstring says "Returns None or one of valid query states."
EmrContainerHook.poll_query_status handles that case explicitly, logging "Try %s: Invalid query state. Retrying again", and once max_polling_attempts is reached it does return query_state, which is None.
poke() finds None in neither tuple and returns True.

So a ThrottlingException, or any transient DescribeJobRun failure that outlasts max_retries, marks the Airflow task successful and releases every downstream task while the EMR on EKS job is still running. The same applies to any job-run state AWS adds that Airflow does not yet know about.

Worth noting the precondition: reaching the None return requires max_retries to be set on the sensor, since that is what bounds the hook's polling loop. Left unset, the loop spins indefinitely instead of returning.

The change
diff
if state in self.INTERMEDIATE_STATES:
return False

  •    return True
    
  •    return state in self.SUCCESS_STATES
    

Why the existing tests did not catch this

test_emr_containers.py covers all seven documented EMR on EKS states: PENDING, SUBMITTED, RUNNING, COMPLETED, FAILED, CANCELLED, CANCEL_PENDING. Every one behaves correctly today. Nothing covered the None that the hook is documented to return, or an unrecognised state.

This PR adds test_poke_unknown_state and test_poke_unrecognised_state to close that gap.

Test results

Before the fix:

test_poke_unknown_state FAILED
test_poke_unrecognised_state FAILED
2 failed, 9 passed

with the hook logging Try 1: Invalid query state. Retrying again and Try 1: Query is still in non-terminal state - SOME_FUTURE_STATE respectively, immediately before poke() returned True.

After the fix:

11 passed
Behaviour change worth calling out

An unknown state now makes the sensor keep poking until its own timeout rather than succeed immediately. That is the minimal, fail-safe change, but the alternative would be to raise on a state that is in none of the three tuples so the failure is loud instead of a timeout. Happy to change it to whichever the maintainers prefer.


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

Generated-by: Claude following the guidelines

Gen-AI disclosure

I used Claude to assist with this contribution. The defect was found by reviewing provider modules that carry no unit tests; the fix and the two added tests were drafted with its help. I verified the call chain through EmrContainerHook.check_query_status and poll_query_status myself, and
ran the test file locally both with and without the fix (2 failed / 9 passed before, 11 passed after). I understand the change and take responsibility for it.

EmrContainerSensor.poke() returned True for any state that was neither a
failure state nor an intermediate state, so an unknown state was treated
as a successful job run. SUCCESS_STATES was declared but never used.

EmrContainerHook.check_query_status returns None when it swallows a
generic ClientError, and poll_query_status passes that None through once
max_polling_attempts is reached. A throttled or otherwise transiently
failing DescribeJobRun therefore marked the task successful while the job
was still running.

Check state against SUCCESS_STATES instead, and add tests for the None
and unrecognised-state cases.
@Priyaj11
Priyaj11 requested a review from o-nikolas as a code owner September 4, 2026 05:01
@boring-cyborg boring-cyborg Bot added area:providers provider:amazon AWS/Amazon - related issues labels Sep 4, 2026
@boring-cyborg

boring-cyborg Bot commented Sep 4, 2026

Copy link
Copy Markdown

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
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

@vincbeck
vincbeck merged commit 90354f1 into apache:main Sep 4, 2026
83 checks passed
@boring-cyborg

boring-cyborg Bot commented Sep 4, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

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.

2 participants