fix: Synchronize provider state access on the state lock - #54
Open
kinyoklion wants to merge 1 commit into
Open
Conversation
Co-Authored-By: rlamb@launchdarkly.com <4955475+kinyoklion@users.noreply.github.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getState()synchronized on thestatefield's value instead of thestateLockmonitor, so reads were never mutually exclusive with writes.getState()now locksstateLock, matchingsetStateinitializesets the ready state throughsetStateand reads it throughgetStateinstead of touching the field unlocked@cursor review
Implementation details
Root cause
synchronized (state)locks on theProviderStateenum constant currently referenced by the field. A reader holding the monitor ofProviderState.NOT_READYdoes not exclude a writer holdingstateLock, so state transitions had no happens-before relationship with reads. Because enum constants are JVM-wide singletons, it also meant contending on a monitor shared with any other code that happens to lock the same constant.initializealso wrotestate = ProviderState.READYand later readstatewithout any lock, on a different thread than the data source status listener that mutates it.Alternatives considered
Making the field
volatilewould fix visibility, but theVALIDbranch ofhandleDataSourceStatusneeds a compare-and-set over the field, so the lock is still required; keeping a single lock is simpler than mixing both.Testing
./gradlew test— all 44 tests pass. The existingLifeCycleTestcases already assert the state transitions this lock protects (NOT_READY→READY, and →ERRORon a failed data source); a test cannot deterministically observe the previous incorrect locking, so no new test is added.No visual preview applies — this is a server-side provider change.
Link to Devin session: https://app.devin.ai/sessions/0c452d209ec54b068ba120b4c92b8f6c
Requested by: @kinyoklion
Note
Overview
Fixes incorrect locking around the OpenFeature provider lifecycle state so reads and writes use the same monitor.
getState()now synchronizes onstateLockinstead of on theProviderStateenum value held in the field, which did not exclude concurrent updates fromsetStateand could contend on JVM-wide enum monitors.initializeroutes the early-ready path throughsetState(READY)and the readiness check throughgetState()instead of assigning or reading thestatefield without the lock.Callers should see the same transitions, with correct visibility and mutual exclusion when the data source listener updates state on another thread.
Reviewed by Cursor Bugbot for commit dfc2c89. Bugbot is set up for automated code reviews on this repo. Configure here.