-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 race in DemandBuffer #3475
Fix race in DemandBuffer #3475
Conversation
|
||
final class DemandBuffer<S: Subscriber>: @unchecked Sendable { | ||
private var buffer = [S.Input]() | ||
private let subscriber: S | ||
private var completion: Subscribers.Completion<S.Failure>? | ||
private var demandState = Demand() | ||
private let lock: os_unfair_lock_t | ||
private let lock = NSRecursiveLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again, I wanted to err on the side of caution and changed this to a recursive lock to avoid deadlocks — but it might be worth reasoning about the code more thoroughly and determining if it's possible to change this back. The main concern is that there are calls to Subscriber.receive
while the lock is held, and afaik these can invoke arbitrary code that might call back into the DemandBuffer
and deadlock us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this does seem to be correct to me. Nice find, Kabir!
Indeed seems like an important fix to add. Great job @kabiroberai on catching this and #3447 ! |
CI failure seems unrelated to the PR 🤔 > sudo xcode-select -s /Applications/Xcode_16.0.app
xcode-select: error: invalid developer directory '/Applications/Xcode_16.0.app' |
GitHub may have changed their images, Xcode 16.0 is now named Xcode_16.app, with a symlink to Xcode_16.0.app, which I guess doesn't work for setting the directory. |
See also: CombineCommunity/CombineExt#90 Co-authored-by: Stephen Celis <stephen@stephencelis.com> # Conflicts: # Sources/ComposableArchitecture/Internal/Create.swift
After landing #3447, we found another race condition — this time in
DemandBuffer
, which is marked@unchecked Sendable
(and should indeed be thread safe) but has no synchronization aroundDemandBuffer.buffer
(as well as other slightly less critical properties) in some spots.A similar fix was actually applied to the original
CombineExt.DemandBuffer
impl back in 2021, but wasn't ported over to TCA. See CombineCommunity/CombineExt#90.