diff --git a/Sources/AsyncAlgorithms/CombineLatest/CombineLatestStateMachine.swift b/Sources/AsyncAlgorithms/CombineLatest/CombineLatestStateMachine.swift index fb68f07..34f4dea 100644 --- a/Sources/AsyncAlgorithms/CombineLatest/CombineLatestStateMachine.swift +++ b/Sources/AsyncAlgorithms/CombineLatest/CombineLatestStateMachine.swift @@ -585,7 +585,9 @@ struct CombineLatestStateMachine< mutating func cancelled() -> CancelledAction? { switch self.state { case .initial: - preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()") + state = .finished + + return .none case .waitingForDemand(let task, let upstreams, _): // The downstream task got cancelled so we need to cancel our upstream Task diff --git a/Sources/AsyncAlgorithms/Debounce/DebounceStateMachine.swift b/Sources/AsyncAlgorithms/Debounce/DebounceStateMachine.swift index cd6cc1a..bd111ab 100644 --- a/Sources/AsyncAlgorithms/Debounce/DebounceStateMachine.swift +++ b/Sources/AsyncAlgorithms/Debounce/DebounceStateMachine.swift @@ -583,9 +583,8 @@ struct DebounceStateMachine { mutating func cancelled() -> CancelledAction? { switch self.state { case .initial: - // Since we are transitioning to `merging` before we return from `makeAsyncIterator` - // this can never happen - preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()") + state = .finished + return .none case .waitingForDemand: // We got cancelled before we event got any demand. This can happen if a cancelled task diff --git a/Sources/AsyncAlgorithms/Merge/MergeStateMachine.swift b/Sources/AsyncAlgorithms/Merge/MergeStateMachine.swift index 57d43bd..e3bb59e 100644 --- a/Sources/AsyncAlgorithms/Merge/MergeStateMachine.swift +++ b/Sources/AsyncAlgorithms/Merge/MergeStateMachine.swift @@ -465,9 +465,11 @@ struct MergeStateMachine< mutating func cancelled() -> CancelledAction { switch state { case .initial: - // Since we are transitioning to `merging` before we return from `makeAsyncIterator` - // this can never happen - preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()") + // Since we are only transitioning to merging when the task is started we + // can be cancelled already. + state = .finished + + return .none case let .merging(task, _, upstreamContinuations, _, .some(downstreamContinuation)): // The downstream Task got cancelled so we need to cancel our upstream Task diff --git a/Sources/AsyncAlgorithms/Zip/ZipStateMachine.swift b/Sources/AsyncAlgorithms/Zip/ZipStateMachine.swift index d6b0c9c..475457c 100644 --- a/Sources/AsyncAlgorithms/Zip/ZipStateMachine.swift +++ b/Sources/AsyncAlgorithms/Zip/ZipStateMachine.swift @@ -460,7 +460,9 @@ struct ZipStateMachine< mutating func cancelled() -> CancelledAction? { switch self.state { case .initial: - preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()") + state = .finished + + return .none case .waitingForDemand(let task, let upstreams): // The downstream task got cancelled so we need to cancel our upstream Task diff --git a/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift b/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift index 489252a..f33258c 100644 --- a/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift +++ b/Tests/AsyncAlgorithmsTests/TestCombineLatest.swift @@ -318,6 +318,16 @@ final class TestCombineLatest2: XCTestCase { task.cancel() wait(for: [finished], timeout: 1.0) } + + func test_combineLatest_when_cancelled() async { + let t = Task { + try? await Task.sleep(nanoseconds: 1_000_000_000) + let c1 = Indefinite(value: "test1").async + let c2 = Indefinite(value: "test1").async + for await _ in combineLatest(c1, c2) {} + } + t.cancel() + } } final class TestCombineLatest3: XCTestCase { diff --git a/Tests/AsyncAlgorithmsTests/TestDebounce.swift b/Tests/AsyncAlgorithmsTests/TestDebounce.swift index d82105b..5d29605 100644 --- a/Tests/AsyncAlgorithmsTests/TestDebounce.swift +++ b/Tests/AsyncAlgorithmsTests/TestDebounce.swift @@ -68,4 +68,15 @@ final class TestDebounce: XCTestCase { let throwingDebounce = [1].async.map { try throwOn(2, $0) }.debounce(for: .zero, clock: ContinuousClock()) for try await _ in throwingDebounce {} } + + func test_debounce_when_cancelled() async throws { + guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") } + + let t = Task { + try? await Task.sleep(nanoseconds: 1_000_000_000) + let c1 = Indefinite(value: "test1").async + for await _ in c1.debounce(for: .seconds(1), clock: .continuous) {} + } + t.cancel() + } } diff --git a/Tests/AsyncAlgorithmsTests/TestMerge.swift b/Tests/AsyncAlgorithmsTests/TestMerge.swift index ef72d1a..0bcb347 100644 --- a/Tests/AsyncAlgorithmsTests/TestMerge.swift +++ b/Tests/AsyncAlgorithmsTests/TestMerge.swift @@ -191,6 +191,16 @@ final class TestMerge2: XCTestCase { task.cancel() wait(for: [finished], timeout: 1.0) } + + func test_merge_when_cancelled() async { + let t = Task { + try? await Task.sleep(nanoseconds: 1_000_000_000) + let c1 = Indefinite(value: "test1").async + let c2 = Indefinite(value: "test1").async + for await _ in merge(c1, c2) {} + } + t.cancel() + } } final class TestMerge3: XCTestCase { diff --git a/Tests/AsyncAlgorithmsTests/TestZip.swift b/Tests/AsyncAlgorithmsTests/TestZip.swift index d27dd73..25bc9ec 100644 --- a/Tests/AsyncAlgorithmsTests/TestZip.swift +++ b/Tests/AsyncAlgorithmsTests/TestZip.swift @@ -158,6 +158,16 @@ final class TestZip2: XCTestCase { task.cancel() wait(for: [finished], timeout: 1.0) } + + func test_zip_when_cancelled() async { + let t = Task { + try? await Task.sleep(nanoseconds: 1_000_000_000) + let c1 = Indefinite(value: "test1").async + let c2 = Indefinite(value: "test1").async + for await _ in zip(c1, c2) {} + } + t.cancel() + } } final class TestZip3: XCTestCase {