Skip to content

fix: preserve cancellation during async flag evaluation - #693

Open
frajberrima wants to merge 1 commit into
open-feature:mainfrom
frajberrima:fix/async-evaluation-cancellation
Open

frajberrima wants to merge 1 commit into
open-feature:mainfrom
frajberrima:fix/async-evaluation-cancellation

Conversation

@frajberrima

@frajberrima frajberrima commented Dec 2, 2026 •

Copy link
Copy Markdown

Cancelling an in-progress async flag evaluation raises UnboundLocalError from final-hook dispatch because the provider await exits before flag_evaluation is assigned. Since asyncio.CancelledError derives from BaseException, the ordinary exception handlers do not initialize those details. The same failure prevents asyncio.wait_for from reporting its expected timeout.

Handle cancellation explicitly, construct default-valued error details for final hooks, and re-raise the original cancellation. The application still receives cancellation; the default is only cleanup metadata. Ordinary provider-error handling is unchanged.

Eight regression cases cover all supported value shapes, the exact provider-raised cancellation object without hooks, and actual task timeout behavior. They fail before the patch and pass afterward. Events synchronize provider startup without sleep-based races.

Validation on Windows: all 234 pytest tests and all 21 Gherkin scenarios pass on Python 3.10.22, 3.11.18, 3.12.16, 3.13.18, 3.14.10 and 3.15.2. Ruff, formatting, repository text/configuration hooks, wheel and sdist builds pass. Strict mypy passes with --num-workers 0 because parallel workers encounter a Windows IPC limitation. Gherkin uses the pinned specification fixtures; live flagd integration and hosted Linux CI have not been run.

The proposed cancellation policy calls final hooks with Reason.ERROR and ErrorCode.GENERAL, but does not call ordinary error hooks for cancellation. The specification does not separately define Python cancellation semantics, so this policy needs maintainer review.

This contribution was prepared with AI assistance. The submitting author has reviewed the contribution and confirmed the DCO attestation; the commit includes their Signed-off-by line. Hosted build, lint, and test workflows currently await repository approval.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Async flag evaluation now records error details when cancellation occurs, re-raises the cancellation, and continues to run the final hooks. Added tests cover flag types, provider-originated cancellation, and timeout cancellation.

Changes: Async cancellation handling

Layer / File(s) Summary
Cancellation handling and tests
openfeature/client.py, tests/test_async_cancellation.py
Async evaluation records error details when cancelled and re-raises CancelledError. Tests check task cancellation, hook calls, and the details passed to finally_after.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: gruebel, toddbaert, aepfli

Merge Risk: Minimal · up to a625c

Cancelling an async flag evaluation no longer fails with UnboundLocalError in final hooks, and the original cancellation still reaches the application. The change is narrow and tested, so merge risk is minimal. Maintainers should confirm the cancellation hook policy.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: preserving cancellation during async flag evaluation. It matches the summary, which describes cancellation handling in evaluate_flag_details_async that re-raises async…
Description check Passed The description matches the changeset. It explains the UnboundLocalError during cancellation of async flag evaluation, the fix that builds default error details and re-raises the cancellation, the reg…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: preserving cancellation during async flag evaluation. It matches the summary, which describes cancellation handling in evaluate_flag_details_async that re-raises asyncio.CancelledError after building default error details for final hooks. The title is short, specific, and has no file list or vague wording.

Full details: Description check

Explanation

The description matches the changeset. It explains the UnboundLocalError during cancellation of async flag evaluation, the fix that builds default error details and re-raises the cancellation, the regression tests, and the cancellation hook policy. It is detailed and on topic.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f753bc16-7a81-47e6-90a5-0bce03127a97
Commits

Reviewing files that changed from the base of the PR and between efd29b1 and a625c31.

Files selected for processing: 2

  • openfeature/client.py
  • tests/test_async_cancellation.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Prepared with AI assistance; reviewed and DCO attestation confirmed by the submitting author.

Signed-off-by: frajberrima <frajberrima98@gmail.com>
@frajberrima
frajberrima force-pushed the fix/async-evaluation-cancellation branch from d6bc3f6 to a625c31 Compare December 2, 2026 21:11
@frajberrima
frajberrima marked this pull request as ready for review December 3, 2026 07:52
@frajberrima
frajberrima requested review from a team as code owners December 3, 2026 07:52

@EmiRZx EmiRZx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tested with asyncio.wait_for and a provider that sleeps. on main the timeout surfaces as UnboundLocalError and no finally hook runs. here it raises TimeoutError. the finally hook gets the default value with GENERAL. a provider that raises CancelledError itself behaves the same. the new tests fail on main and the suite passes. it merges cleanly with #691.

a KeyboardInterrupt from the provider still ends as UnboundLocalError. the sync path does the same. setting flag_evaluation before the try would cover every BaseException. that could be a follow-up.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants