Skip to content

[Python] Fail bundles on state write errors - #39992

Merged
shunping merged 4 commits into
apache:masterfrom
bvolpato:bvolpato/python-state-write-errors
Oct 19, 2026
Merged

shunping merged 4 commits into
apache:masterfrom
bvolpato:bvolpato/python-state-write-errors

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Python runtime state commits ignore StateResponse.error, so a runner-reported write failure can still complete the bundle successfully. When an append follows a clear, the clear future is overwritten and its response is never checked.

This change checks pending write responses before completing bag, set, and ordered-list state commits, and retains both futures when an append follows a clear. Write errors fail the bundle through the existing failure handling; successful commits send the same state requests.

Local worker validation passed, and the regressions fail against the unfixed implementation. Run the Python unit suite with:

./gradlew :sdks:python:test-suites:tox:py312:testPython312

When cross-bundle user-state caching is enabled, a retry needs a runner-supplied cache token that prevents reuse of state cached by the failed bundle.


  • If this contribution is large, please file an Apache Individual Contributor License Agreement.
  • Replace these instructions with a description of your change and why you have made it.
  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.

See CONTRIBUTING.md for contribution guidelines.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.74%. Comparing base (76f6e6d) to head (70f308b).
⚠️ Report is 216 commits behind head on master.

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #39992       +/-   ##
=============================================
+ Coverage     56.16%   79.74%   +23.57%     
=============================================
  Files          1124      487      -637     
  Lines        178336    82280    -96056     
  Branches       1489        0     -1489     
=============================================
- Hits         100170    65613    -34557     
+ Misses        75645    16667    -58978     
+ Partials       2521        0     -2521     
Flag Coverage Δ
go ?
java ?
python 79.74% <100.00%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bvolpato
bvolpato marked this pull request as ready for review October 18, 2026 01:11

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

📋 Review Summary

This PR fixes a silent data-loss bug in Python portable worker state handling by ensuring that state write (append and clear) response errors are checked during commit() across SynchronousBagRuntimeState, SynchronousSetRuntimeState, and SynchronousOrderedListRuntimeState. It also properly awaits both the clear and extend futures in SynchronousBagRuntimeState when a state is cleared and then appended to within the same bundle, and upgrades _DeferredCall.get() in sdk_worker.py to propagate error responses across chunked state appends.

🔍 General Feedback

  • The shared _await_state_commit helper and the updated _DeferredCall.get() logic cleanly and consistently surface runner-reported StateResponse.error messages across all three user state types.
  • Unit test coverage in RuntimeStateCommitTest is thorough, covering single append/clear failures, clear-then-append sequences, multi-chunk extend failures, and set compaction failures.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@shunping

Copy link
Copy Markdown
Collaborator

/gemini review

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

📋 Review Summary

This PR fixes a critical reliability issue in the Python SDK harness where runner-reported state write errors (StateResponse.error) were ignored during state commit, and clear futures in SynchronousBagRuntimeState were overwritten when followed by an extend call. The introduction of _await_state_commit and the updated future tracking across SynchronousBagRuntimeState, SynchronousSetRuntimeState, and SynchronousOrderedListRuntimeState are clean, well-structured, and thoroughly covered by unit tests.

🔍 General Feedback

  • The consolidation of future awaiting and error checking into _await_state_commit cleanly handles both single and multi-step state mutations (e.g., clear followed by append, set compaction, and chunked appends).
  • Consider propagating WorkCancelledException when StateResponse.error_reason == CANCELLED to stay consistent with GrpcStateHandler._blocking_request.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

response.error for response in responses if response and response.error
]
if errors:
raise RuntimeError('\n'.join(errors))

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.

🟢 Nit: In GrpcStateHandler._blocking_request (sdk_worker.py), state responses with response.error_reason == beam_fn_api_pb2.StateResponse.ErrorReason.CANCELLED raise WorkCancelledException rather than RuntimeError so that SdkHarness._execute logs the cancellation at INFO level instead of ERROR. Consider checking error_reason here (and propagating it in GlobalCachingStateHandler.extend's _DeferredCall) for consistency with state read error handling.

@shunping shunping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

@shunping
shunping merged commit 3bef0c0 into apache:master Oct 19, 2026
89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants