mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-26 04:37:48 +00:00
asyncChannel: fixes potential crashes when no more awaiting (#143)
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:
1 parent
d2164b9ef6
commit
047ab6abef
1 file changed
+10
-2
@@ -89,7 +89,11 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
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)])
|
||||
@@ -145,7 +149,11 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
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()
|
||||
|
||||
Reference in new issue
Block a user