Skip to content

Commit b58ff91

Browse files
author
Thibault Wittemberg
committed
asyncChannel: fixes potential crashes when no more awaiting
This commit fixes crashes where the state is "awaiting" with no more awaiting clients. The state is now set to .idle to reflect the reality.
1 parent d2164b9 commit b58ff91

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/AsyncAlgorithms/AsyncChannel.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
8989
switch self {
9090
case .awaiting(var awaiting):
9191
let continuation = awaiting.remove(Awaiting(placeholder: generation))?.continuation
92-
self = .awaiting(awaiting)
92+
if awaiting.isEmpty {
93+
self = .idle
94+
} else {
95+
self = .awaiting(awaiting)
96+
}
9397
return continuation
9498
case .idle:
9599
self = .awaiting([Awaiting(cancelled: generation)])
@@ -145,7 +149,11 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
145149
nexts.remove(Awaiting(placeholder: generation))
146150
cancelled = true
147151
}
148-
state.emission = .awaiting(nexts)
152+
if nexts.isEmpty {
153+
state.emission = .idle
154+
} else {
155+
state.emission = .awaiting(nexts)
156+
}
149157
return nil
150158
}
151159
}?.resume()

0 commit comments

Comments
 (0)