Skip to content

feat: provider wrapper to support state race fix#487

Draft
toddbaert wants to merge 1 commit intomainfrom
poc/fix-provider-init-race
Draft

feat: provider wrapper to support state race fix#487
toddbaert wants to merge 1 commit intomainfrom
poc/fix-provider-init-race

Conversation

@toddbaert
Copy link
Copy Markdown
Member

@toddbaert toddbaert commented Apr 8, 2026

PoC for open-feature/spec#365. This adds a StateManagingProvider interface (opt-in) so providers can own their state and event emissions, fixing the race between SDK-managed state writes and provider-emitted events during/after init. When a provider implements this interface, the SDK reads state directly from the provider and skips its own state writes and event emissions in the init, shutdown, and runtime event paths. Legacy providers that do not implement the interface continue to work exactly as before (SDK manages their state). Not breaking for application-authors or provider-authors.

Companion PoCs:

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.88%. Comparing base (4ae2f18) to head (a87f422).

Files with missing lines Patch % Lines
openfeature/event_executor.go 75.00% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
- Coverage   84.07%   83.88%   -0.19%     
==========================================
  Files          27       27              
  Lines        2079     2098      +19     
==========================================
+ Hits         1748     1760      +12     
- Misses        292      295       +3     
- Partials       39       43       +4     
Flag Coverage Δ
e2e 83.88% <82.14%> (-0.19%) ⬇️
unit 83.88% <82.14%> (-0.19%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 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.

Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the StateManagingProvider interface, allowing providers to manage their own state and event emissions, thereby decoupling them from the SDK's shadow state management. The changes include updates to eventExecutor and evaluationAPI to respect this interface, as well as caching the capability in providerReference. I have provided feedback regarding the optimization of type assertions by utilizing the new delegateManagesState field and a recommendation to avoid holding the executor mutex during external provider calls to prevent potential deadlocks.

Comment on lines +155 to +157
if smp, ok := providerReference.featureProvider.(StateManagingProvider); ok {
state = smp.State()
} else {
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.

medium

Since providerReference now includes the delegateManagesState flag, we can use it here to avoid the type assertion check, improving clarity and consistency with the new field.

Suggested change
if smp, ok := providerReference.featureProvider.(StateManagingProvider); ok {
state = smp.State()
} else {
if providerReference.delegateManagesState {
state = providerReference.featureProvider.(StateManagingProvider).State()
} else {

Comment on lines +195 to +207
e.mu.Lock()
defer e.mu.Unlock()

// find the provider reference for this domain
ref, ok := e.namedProviderReference[domain]
if !ok {
ref = e.defaultProviderReference
}

// state-managing providers own their state; read directly
if smp, ok := ref.featureProvider.(StateManagingProvider); ok {
return smp.State()
}
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.

medium

Holding the executor's mutex while calling smp.State() is risky as it involves calling into external provider code while holding a lock. This can lead to performance bottlenecks or potential deadlocks if the provider's State() method blocks or attempts to call back into the SDK. Since we only need the lock to retrieve the provider reference, we can release it early. Additionally, we can leverage the delegateManagesState flag.

	e.mu.Lock()
	// find the provider reference for this domain
	ref, ok := e.namedProviderReference[domain]
	if !ok {
		ref = e.defaultProviderReference
	}
	e.mu.Unlock()

	// state-managing providers own their state; read directly
	if ref.delegateManagesState {
		return ref.featureProvider.(StateManagingProvider).State()
	}

e.mu.Lock()
defer e.mu.Unlock()

_, delegateManagesState := handler.(StateManagingProvider)
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.

medium

This type assertion is redundant because the providerReference already caches this information in the delegateManagesState field. You can remove this line and use the flag from the references in the subsequent logic (e.g., reference.delegateManagesState and e.defaultProviderReference.delegateManagesState).

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.

1 participant