Skip to content

Commit 6d5d631

Browse files
author
Thibault Wittemberg
authored
asyncThrowingChannel: fixes potential crashes when no more awaiting (#150)
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 047ab6a commit 6d5d631

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/AsyncAlgorithms/AsyncThrowingChannel.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
8888
switch self {
8989
case .awaiting(var awaiting):
9090
let continuation = awaiting.remove(Awaiting(placeholder: generation))?.continuation
91-
self = .awaiting(awaiting)
91+
if awaiting.isEmpty {
92+
self = .idle
93+
} else {
94+
self = .awaiting(awaiting)
95+
}
9296
return continuation
9397
case .idle:
9498
self = .awaiting([Awaiting(cancelled: generation)])
@@ -143,7 +147,11 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
143147
nexts.remove(Awaiting(placeholder: generation))
144148
cancelled = true
145149
}
146-
state.emission = .awaiting(nexts)
150+
if nexts.isEmpty {
151+
state.emission = .idle
152+
} else {
153+
state.emission = .awaiting(nexts)
154+
}
147155
return nil
148156
}
149157
}?.resume()

0 commit comments

Comments
 (0)