From ca2ac6049500a7275e7db93b6b2bbb7a88dbaf5a Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Mon, 6 Feb 2023 17:05:07 +0000 Subject: [PATCH] Tolerate cancelling before we create our task (#250) # Motivation We switched the point in time when we create tasks to the first call to `next()`. This had the side effect that we can now be cancelled before we even created our task. Which by itself is fine but we didn't handle it in the state machines. # Modification Adapt the state machines of `debounce`, `zip`, `merge` and `combineLatest` to handle cancellation before `next()`. # Result We shouldn't run into these preconditions anymore. --- .../CombineLatest/CombineLatestStateMachine.swift | 4 +++- .../Debounce/DebounceStateMachine.swift | 5 ++--- Sources/AsyncAlgorithms/Merge/MergeStateMachine.swift | 8 +++++--- Sources/AsyncAlgorithms/Zip/ZipStateMachine.swift | 4 +++- Tests/AsyncAlgorithmsTests/TestCombineLatest.swift | 10 ++++++++++ Tests/AsyncAlgorithmsTests/TestDebounce.swift | 11 +++++++++++ Tests/AsyncAlgorithmsTests/TestMerge.swift | 10 ++++++++++ Tests/AsyncAlgorithmsTests/TestZip.swift | 10 ++++++++++ 8 files changed, 54 insertions(+), 8 deletions(-) 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 {