Skip to content

AsyncChannel fixes potential crashes when no more awaiting #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/AsyncAlgorithms/AsyncChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down Expand Up @@ -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()
Expand Down