Skip to content

Commit

Permalink
asyncChannel: fixes potential crashes when no more awaiting (#143)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
twittemb committed Apr 15, 2022
1 parent d2164b9 commit 047ab6a
Showing 1 changed file with 10 additions and 2 deletions.
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

0 comments on commit 047ab6a

Please sign in to comment.