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 race in DemandBuffer #3475

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

kabiroberai
Copy link
Contributor

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 around DemandBuffer.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.


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()
Copy link
Contributor Author

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.

Copy link

@jpsim jpsim left a 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!

@freak4pc
Copy link
Contributor

freak4pc commented Nov 8, 2024

Indeed seems like an important fix to add. Great job @kabiroberai on catching this and #3447 !

@kabiroberai
Copy link
Contributor Author

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'

@jshier
Copy link
Contributor

jshier commented Nov 9, 2024

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.

@stephencelis stephencelis merged commit 8784f76 into pointfreeco:main Nov 12, 2024
13 checks passed
ChloeMayhewELT pushed a commit to ChloeMayhewELT/swift-composable-architecture that referenced this pull request Jan 8, 2025
See also: CombineCommunity/CombineExt#90

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
# Conflicts:
#	Sources/ComposableArchitecture/Internal/Create.swift
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.

5 participants