Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReplaySubject race condition #138

Merged

Conversation

mruston-heb
Copy link
Contributor

The ReplaySubject currently adds a new subscription before replaying the buffer. Since these two steps are not synchronized by the lock, it is possible that a new value is sent to the new subscription before the new subscription finishes replaying the buffer. This can result in a new subscription to a ReplaySubject receiving values out of order.

To fix this, it should be safe to have the new subscription replay the buffer before being added to the collection of subscriptions. The new subscription's DemandBuffer will have no requested demand at this point, so it will hold onto the replayed values until demand is requested. Once the lock is unlocked, any new values will be sent to all the subscriptions, which will always be added to the demand buffer after the replayed values.

I tried to keep the footprint of the new test minimal, but let me know if additional comments or helper methods could make it more clear.

…e locked to ensure new values are sent after the replayed values.
@codecov
Copy link

codecov bot commented Jul 23, 2022

Codecov Report

Merging #138 (3651dd5) into main (5818492) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #138      +/-   ##
==========================================
+ Coverage   95.53%   95.55%   +0.02%     
==========================================
  Files          68       68              
  Lines        3833     3851      +18     
==========================================
+ Hits         3662     3680      +18     
  Misses        171      171              
Impacted Files Coverage Δ
Sources/Subjects/ReplaySubject.swift 100.00% <100.00%> (ø)
Tests/ReplaySubjectTests.swift 99.64% <100.00%> (+0.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5818492...3651dd5. Read the comment docs.

Copy link
Member

@freak4pc freak4pc left a comment

Choose a reason for hiding this comment

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

Very interesting. Thanks for the fix :)

@freak4pc freak4pc merged commit 1c05161 into CombineCommunity:main Jul 23, 2022
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