Skip to content

fix: store context parameter in initialize for segment evaluation #41

Merged
dylanlerch merged 2 commits intomainfrom
fix/initialize-context-parameter
Mar 4, 2026
Merged

fix: store context parameter in initialize for segment evaluation #41
dylanlerch merged 2 commits intomainfrom
fix/initialize-context-parameter

Conversation

@enf0rc3
Copy link
Contributor

@enf0rc3 enf0rc3 commented Feb 19, 2026

Background

We identified a race condition during between setProviderAndWait and setContext for segment-based targeting.

The race condition happened like this:

  1. await OpenFeature.setProviderAndWait(provider) - provider becomes READY, this.context is {}
  2. await OpenFeature.setContext({serverUri: ...}) - calls onContextChange, sets this.context

Between steps 1 and 2, the provider is READY and React hooks immediately evaluate flags.

The React SDK's useBooleanFlagValue hook attaches ContextChanged event handlers in useEffect (which runs after render).

If the ContextChanged event from step 2 fires before useEffect attaches handlers, the hook never re-evaluates.

The initial evaluation uses this.context = {}, so matchesSegment() returns false and the toggle appears disabled.

So in summary, it fixes these 2 bugs.

  1. Library bug: initialize() ignored the context parameter entirely, so this.context was always {} regardless of how you called the SDK
  2. Race condition: The consumer code called setContext after setProviderAndWait, creating a window where evaluations happen with no context

Because of the library bug even using "correct" calling order of (setting context before the provider) didn't work - the context was passed to initialize but thrown away.

Result

  • Updated initialize() to take a context parameter passed by the OpenFeature SDK and update the context
  • Updated README examples to show the correct calling patterns

initialize() ignored the context parameter passed by the OpenFeature SDK,
causing segment-based toggles to fall back to their default value on initial
load. This created a race condition where flag evaluations between provider
READY and a separate setContext call would fail segment matching.
@enf0rc3 enf0rc3 changed the title fix: store context parameter in initialize for segment evaluation Fix store context parameter in initialize for segment evaluation Feb 19, 2026
@enf0rc3 enf0rc3 changed the title Fix store context parameter in initialize for segment evaluation fix: store context parameter in initialize for segment evaluation Feb 19, 2026
Copy link
Contributor

@dylanlerch dylanlerch left a comment

Choose a reason for hiding this comment

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

Changes look good, tests are amazing. Thanks for finding this.

Just a request for some changes to simplify the docs. I can sort these changes if you want, just let me know.

@dylanlerch dylanlerch merged commit 4e5dff4 into main Mar 4, 2026
7 checks passed
@dylanlerch dylanlerch deleted the fix/initialize-context-parameter branch March 4, 2026 05:03
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