Repository navigation
fix: close first-read race in RxState, RxSuspension and RxCacheSize - #4519
Merged
Merged
Conversation
RxState.DefaultExceptionHandler, RxSuspension.SuspensionHost and the RxCacheSize limits set an "initialized" flag before assigning the value, so a thread racing the first read could see the flag and return null (or 0 for the cache limits). Passing null to the internal initializers also left the flag set and the value null for good. Each value is now its own initialization flag: it is read with Volatile.Read and published once, with Interlocked.CompareExchange for the handler and the cache limits (which now travel together in one immutable object) and under a lock for the suspension host so only one default host is created. The first value published still wins, and null arguments are rejected before any state changes. Closes #4517
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4519 +/- ##
==========================================
+ Coverage 93.62% 93.68% +0.05%
==========================================
Files 175 175
Lines 6227 6220 -7
Branches 743 741 -2
==========================================
- Hits 5830 5827 -3
+ Misses 396 392 -4
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
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.



Summary
RxState.DefaultExceptionHandler,RxSuspension.SuspensionHostand theRxCacheSizelimits never return an unset value, however many threads read them first.RxState.DefaultExceptionHandlernever returns null. The handler is published once, atomically, and the first handler published (the builder's or the default) wins, as before.RxSuspension.SuspensionHostnever returns null. The host is published once under a lock, so only one default host is created.RxCacheSize.SmallCacheLimitandBigCacheLimitnever return 0. The two limits are published together, so a reader never sees one set and the other not.Why
Each of these set its "initialized" flag before it assigned the value, so a thread racing the first read could see the flag and return the unassigned field.
RxState.DefaultExceptionHandler!.OnNext(...), which dereferences that null..Reactiveflavours, which compile the sameReactiveUI.Sharedsource.Closes #4517
Breaking changes
None
How this was verified
New tests race several threads on the first read of each value over many resets, and cover the builder, default and null-argument paths.
main. They run in both the lean and.Reactivetest assemblies.Notes for the reviewer
The three files under
src/ReactiveUI.Sharedare the whole fix; the value field is now its own initialization flag.RxSuspensionuses a lock rather than compare-and-exchange so that a losing thread never builds aSuspensionHost(a disposableReactiveObject) only to throw it away.RxCacheSizeholds both limits in a private immutableCacheLimitsobject so they publish in one step.FirstReadRacein the test infrastructure is the shared barrier harness the three race tests use.Checklist