Skip to content

Commit 29ad29a

Browse files
committed
Fix race condition in AsyncThrowingCurrentValueSubject
1 parent fc0dc63 commit 29ad29a

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

Sources/AsyncSubjects/AsyncThrowingCurrentValueSubject.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: As
9797
) -> (iterator: AsyncThrowingBufferedChannel<Element, Error>.Iterator, unregister: @Sendable () -> Void) {
9898
let asyncBufferedChannel = AsyncThrowingBufferedChannel<Element, Error>()
9999

100-
let (terminalState, current) = self.state.withCriticalRegion { state -> (Termination?, Element) in
101-
(state.terminalState, state.current)
100+
let terminalState = self.state.withCriticalRegion { state -> Termination? in
101+
state.terminalState
102102
}
103103

104104
if let terminalState = terminalState {
@@ -111,11 +111,10 @@ public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: As
111111
return (asyncBufferedChannel.makeAsyncIterator(), {})
112112
}
113113

114-
asyncBufferedChannel.send(current)
115-
116114
let consumerId = self.state.withCriticalRegion { state -> Int in
117115
state.ids += 1
118116
state.channels[state.ids] = asyncBufferedChannel
117+
asyncBufferedChannel.send(state.current)
119118
return state.ids
120119
}
121120

0 commit comments

Comments
 (0)