-
Notifications
You must be signed in to change notification settings - Fork 31
Fix race condition in AsyncCurrentValueSubject
#40
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,8 +97,8 @@ public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: As | |
| ) -> (iterator: AsyncThrowingBufferedChannel<Element, Error>.Iterator, unregister: @Sendable () -> Void) { | ||
| let asyncBufferedChannel = AsyncThrowingBufferedChannel<Element, Error>() | ||
|
|
||
| let (terminalState, current) = self.state.withCriticalRegion { state -> (Termination?, Element) in | ||
| (state.terminalState, state.current) | ||
| let terminalState = self.state.withCriticalRegion { state -> Termination? in | ||
| state.terminalState | ||
| } | ||
|
|
||
| if let terminalState = terminalState { | ||
|
|
@@ -111,11 +111,10 @@ public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: As | |
| return (asyncBufferedChannel.makeAsyncIterator(), {}) | ||
| } | ||
|
|
||
| asyncBufferedChannel.send(current) | ||
|
|
||
| let consumerId = self.state.withCriticalRegion { state -> Int in | ||
| state.ids += 1 | ||
| state.channels[state.ids] = asyncBufferedChannel | ||
| asyncBufferedChannel.send(state.current) | ||
| return state.ids | ||
|
Comment on lines
114
to
118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Calling Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @codex What do you suggest that solves the initial issue while avoiding re-entrency ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To use Codex here, create an environment for this repo. |
||
| } | ||
|
|
||
|
|
||
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.
Calling
asyncBufferedChannel.send(state.current)while insidestate.withCriticalRegioncan synchronously resume an awaiting consumer. If that consumer’s code touches the subject again (e.g., readsvalueor callssend), it will try to re-enter the sameManagedCriticalStatethat uses a non‑reentrantos_unfair_lock, leading to a deadlock. This only occurs when a consumer awaits the channel and then re-enters the subject on receipt, but that pattern is common in for‑await loops.Useful? React with 👍 / 👎.
Uh oh!
There was an error while loading. Please reload this page.
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.
@codex What do you suggest that solves the initial issue while avoiding re-entrency ?
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.
@codex What do you suggest that solves the initial issue while avoiding re-entrency ?
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.
Codex couldn't complete this request. Try again later.