diff --git a/Sources/AsyncAlgorithms/Zip/Zip2Runtime.swift b/Sources/AsyncAlgorithms/Zip/Zip2Runtime.swift index 02cc26b..86e875f 100644 --- a/Sources/AsyncAlgorithms/Zip/Zip2Runtime.swift +++ b/Sources/AsyncAlgorithms/Zip/Zip2Runtime.swift @@ -24,12 +24,6 @@ where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: func next() async rethrows -> (Base1.Element, Base2.Element)? { try await withTaskCancellationHandler { - let output = self.stateMachine.withCriticalRegion { stateMachine in - stateMachine.rootTaskIsCancelled() - } - // clean the allocated resources and state - self.handle(rootTaskIsCancelledOutput: output) - } operation: { let results = await withUnsafeContinuation { continuation in self.stateMachine.withCriticalRegion { stateMachine in let output = stateMachine.newDemandFromConsumer(suspendedDemand: continuation) @@ -59,6 +53,12 @@ where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: } return try (results.0._rethrowGet(), results.1._rethrowGet()) + } onCancel: { + let output = self.stateMachine.withCriticalRegion { stateMachine in + stateMachine.rootTaskIsCancelled() + } + // clean the allocated resources and state + self.handle(rootTaskIsCancelledOutput: output) } } diff --git a/Sources/AsyncAlgorithms/Zip/Zip3Runtime.swift b/Sources/AsyncAlgorithms/Zip/Zip3Runtime.swift index ee39310..63e83fd 100644 --- a/Sources/AsyncAlgorithms/Zip/Zip3Runtime.swift +++ b/Sources/AsyncAlgorithms/Zip/Zip3Runtime.swift @@ -26,12 +26,6 @@ where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: func next() async rethrows -> (Base1.Element, Base2.Element, Base3.Element)? { try await withTaskCancellationHandler { - let output = self.stateMachine.withCriticalRegion { stateMachine in - stateMachine.rootTaskIsCancelled() - } - // clean the allocated resources and state - self.handle(rootTaskIsCancelledOutput: output) - } operation: { let results = await withUnsafeContinuation { continuation in self.stateMachine.withCriticalRegion { stateMachine in let output = stateMachine.newDemandFromConsumer(suspendedDemand: continuation) @@ -61,6 +55,12 @@ where Base1: Sendable, Base1.Element: Sendable, Base2: Sendable, Base2.Element: } return try (results.0._rethrowGet(), results.1._rethrowGet(), results.2._rethrowGet()) + } onCancel: { + let output = self.stateMachine.withCriticalRegion { stateMachine in + stateMachine.rootTaskIsCancelled() + } + // clean the allocated resources and state + self.handle(rootTaskIsCancelledOutput: output) } }