Skip to content

Commit 999b204

Browse files
authored
Merge pull request #20 from swhitty/cancel-task
next() can hang indefinitely if the Task is already cancelled.
2 parents 1286bef + a5bf1be commit 999b204

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Sources/Operators/AsyncSwitchToLatestSequence.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,14 @@ where Base.Element: AsyncSequence, Base: Sendable, Base.Element.Element: Sendabl
218218
}
219219

220220
public mutating func next() async rethrows -> Element? {
221+
guard !Task.isCancelled else { return nil }
221222
self.startBase()
222223

223-
return try await withTaskCancellationHandler { [baseTask] in
224+
return try await withTaskCancellationHandler { [baseTask, state] in
224225
baseTask?.cancel()
226+
state.withCriticalRegion {
227+
$0.childTask?.cancel()
228+
}
225229
} operation: {
226230
while true {
227231
let childTask = await withUnsafeContinuation { [state] (continuation: UnsafeContinuation<Task<ChildValue?, Never>?, Never>) in

0 commit comments

Comments
 (0)