Skip to content

Commit fc0dc63

Browse files
committed
Fix race condition in AsyncCurrentValueSubject
1 parent 1f0729e commit fc0dc63

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

Sources/AsyncSubjects/AsyncCurrentValueSubject.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,19 @@ public final class AsyncCurrentValueSubject<Element>: AsyncSubject where Element
9191
func handleNewConsumer() -> (iterator: AsyncBufferedChannel<Element>.Iterator, unregister: @Sendable () -> Void) {
9292
let asyncBufferedChannel = AsyncBufferedChannel<Element>()
9393

94-
let (terminalState, current) = self.state.withCriticalRegion { state -> (Termination?, Element) in
95-
(state.terminalState, state.current)
94+
let terminalState = self.state.withCriticalRegion { state -> Termination? in
95+
state.terminalState
9696
}
9797

9898
if let terminalState = terminalState, terminalState.isFinished {
9999
asyncBufferedChannel.finish()
100100
return (asyncBufferedChannel.makeAsyncIterator(), {})
101101
}
102102

103-
asyncBufferedChannel.send(current)
104-
105103
let consumerId = self.state.withCriticalRegion { state -> Int in
106104
state.ids += 1
107105
state.channels[state.ids] = asyncBufferedChannel
106+
asyncBufferedChannel.send(state.current)
108107
return state.ids
109108
}
110109

0 commit comments

Comments
 (0)