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.
This commit is contained in:
Thibault Wittemberg authored and GitHub committed 2022-04-21 11:10:14 -07:00
1 parent 047ab6abef
commit 6d5d6319f4
1 file changed
+10 -2
@@ -88,7 +88,11 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
switch self {
case .awaiting(var awaiting):
let continuation = awaiting.remove(Awaiting(placeholder: generation))?.continuation
self = .awaiting(awaiting)
if awaiting.isEmpty {
self = .idle
} else {
self = .awaiting(awaiting)
}
return continuation
case .idle:
self = .awaiting([Awaiting(cancelled: generation)])
@@ -143,7 +147,11 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
nexts.remove(Awaiting(placeholder: generation))
cancelled = true
}
state.emission = .awaiting(nexts)
if nexts.isEmpty {
state.emission = .idle
} else {
state.emission = .awaiting(nexts)
}
return nil
}
}?.resume()