No initial emit under locks in share(replay: 1, scope: .whileConnected)#2654
Merged
freak4pc merged 15 commits intoReactiveX:mainfrom Jan 21, 2026
Merged
No initial emit under locks in share(replay: 1, scope: .whileConnected)#2654freak4pc merged 15 commits intoReactiveX:mainfrom
share(replay: 1, scope: .whileConnected)#2654freak4pc merged 15 commits intoReactiveX:mainfrom
Conversation
… do that under their own lock
share(replay: 1) and Subjects
share(replay: 1) and Subjectsshare(replay: 1) and Subjects
share(replay: 1) and Subjectsshare(replay: 1) and Subjects
Member
|
I'll review this later, but wanted to say I highly appreciate your PR, and the apology 🙌 |
Member
|
Could you add a unit tests that validates the fix against the issue you found? |
0f88e19 to
853faae
Compare
share(replay: 1) and Subjectsshare(replay: , scope: .whileConnected)
share(replay: , scope: .whileConnected)share(replay: any, scope: .whileConnected)
share(replay: any, scope: .whileConnected)share(replay: 1, scope: .whileConnected)
Contributor
Author
|
I have prepared tests to test the specific issue, although, now that I think of it, they could be implemented better:
But the main take away I guess is that it checks for the original issue, despite it intrinsically being impossible to happen. |
Contributor
Author
|
@freak4pc @danielt1263 Can you guys please take a look? |
Contributor
|
It'd be great to get this merged if the trade-offs are acceptable. |
* main: Remove legacy canImport(_Concurrency) checks feat: Enable Swift Concurrency for Linux (ReactiveX#2660) Android support in Package.swift and CI testing (ReactiveX#2663) VirtualTimeScheduler - fix typo (ReactiveX#2671) Fix typo: `MutlicastType` -> `MulticastType` (ReactiveX#2669) Fix Swift 6.2 weak mutability warning in Binder (ReactiveX#2677) Update README.md (ReactiveX#2675) Unify version to xcconfig Update docs Deprecate CocoaPods Fix tests Format cleanup Update tooling Upgrade Xcode 26 Fix race conditions in PrimitiveSequence+Concurrency (ReactiveX#2641) Signing updates
freak4pc
approved these changes
Jan 21, 2026
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.
UPDATE2: unit tests have uncovered another unrelated issue #2655 . This PR provides fix for that too.
UPDATE: turns out, Subjects are not broken, the issue is 100% centralized in the
ShareReplay1WhileConnectedThis pull request aims to fix #2653.
The original issue stems from the fact that:
ShareWhileConnected,ShareReplay1WhileConnectedReplaySubject, BehaviorSubject and PublishSubjectemit their initial events upon subscription while holding onto their internal locks that protect their mutable stateThe last 3 points perfectly describe the behavior of a
zipoperator, which:ShareReplay1WhileConnector anySubject, with these sources retaining their locksIn a multithreaded environment, these conditions can race, and this can produce a deadlock. This PR positions "emission upon subscription under lock" as the core issue and tries to fix specifically this behavior.
Special considerations:
Additionally:
@freak4pc I would like to say sorry for this comment. I really wasn't planning to add anything valuable to the conversation, and my behavior was really counter productive here. This wasn't a troll comment, it was just - I was amused by the fact that RxSwift can break, I found it funny.
@danielt1263 Also I would like to say thank you for your time and the fact that you responded to me.
I would also like to say sorry to you both for this comment - the vagueness of my answer actually permitted an interpretation where I am not only posting nothing of value, but also deliberately try and insult you or the framework. This wasn't my intention, and I'm sorry that I came off this way.
Fixes #2655
Fixes #2653