Skip to content

Switch from group.waitForAll() to group.next() #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ struct CombineLatestStateMachine<
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")

case .upstreamsFinished:
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamThrew()")
// We need to tolerate multiple upstreams failing
return .none

case .waitingForDemand(let task, let upstreams, _):
// An upstream threw. We can cancel everything now and transition to finished.
Expand Down
54 changes: 26 additions & 28 deletions Sources/AsyncAlgorithms/CombineLatest/CombineLatestStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,35 +319,33 @@ final class CombineLatestStorage<
}
}

do {
try await group.waitForAll()
} catch {
// One of the upstream sequences threw an error
self.stateMachine.withCriticalRegion { stateMachine in
let action = stateMachine.upstreamThrew(error)

switch action {
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()

case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()

downstreamContinuation.resume(returning: .failure(error))

case .none:
break
}
}
while !group.isEmpty {
do {
try await group.next()
} catch {
// One of the upstream sequences threw an error
self.stateMachine.withCriticalRegion { stateMachine in
let action = stateMachine.upstreamThrew(error)
switch action {
case .cancelTaskAndUpstreamContinuations(let task, let upstreamContinuations):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()
downstreamContinuation.resume(returning: .failure(error))
case .none:
break
}
}

group.cancelAll()
group.cancelAll()
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AsyncAlgorithms/Debounce/DebounceStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
preconditionFailure("Internal inconsistency current state \(self.state) and received upstreamFinished()")

case .upstreamFailure:
// The upstream already failed so it should never have throw again.
preconditionFailure("Internal inconsistency current state \(self.state) and received childTaskSuspended()")
// We need to tolerate multiple upstreams failing
return .none

case .waitingForDemand(let task, .none, let clockContinuation, .none):
// We don't have any buffered element so we can just go ahead
Expand Down
64 changes: 33 additions & 31 deletions Sources/AsyncAlgorithms/Debounce/DebounceStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,41 @@ final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable
}
}

do {
try await group.waitForAll()
} catch {
// The upstream sequence threw an error
let action = self.stateMachine.withCriticalRegion { $0.upstreamThrew(error) }
while !group.isEmpty {
do {
try await group.next()
} catch {
// One of the upstream sequences threw an error
self.stateMachine.withCriticalRegion { stateMachine in
let action = stateMachine.upstreamThrew(error)
switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuation(
let downstreamContinuation,
let error,
let task,
let upstreamContinuation,
let clockContinuation
):
upstreamContinuation?.resume(throwing: CancellationError())
clockContinuation?.resume(throwing: CancellationError())

switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuation(
let downstreamContinuation,
let error,
let task,
let upstreamContinuation,
let clockContinuation
):
upstreamContinuation?.resume(throwing: CancellationError())
clockContinuation?.resume(throwing: CancellationError())

task.cancel()

downstreamContinuation.resume(returning: .failure(error))

case .cancelTaskAndClockContinuation(
let task,
let clockContinuation
):
clockContinuation?.resume(throwing: CancellationError())
task.cancel()

case .none:
break
}
task.cancel()

downstreamContinuation.resume(returning: .failure(error))

group.cancelAll()
case .cancelTaskAndClockContinuation(
let task,
let clockContinuation
):
clockContinuation?.resume(throwing: CancellationError())
task.cancel()
case .none:
break
}
}

group.cancelAll()
}
}
}
}
Expand Down
63 changes: 31 additions & 32 deletions Sources/AsyncAlgorithms/Merge/MergeStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,40 +404,39 @@ final class MergeStorage<
}
}
}

do {
try await group.waitForAll()
} catch {

while !group.isEmpty {
do {
try await group.next()
} catch {
// One of the upstream sequences threw an error
let action = self.lock.withLock {
self.stateMachine.upstreamThrew(error)
}

switch action {
case let .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
downstreamContinuation,
error,
task,
upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }

task.cancel()

downstreamContinuation.resume(throwing: error)
case let .cancelTaskAndUpstreamContinuations(
task,
upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }

task.cancel()

case .none:
break
let action = self.lock.withLock {
self.stateMachine.upstreamThrew(error)
}
switch action {
case let .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
downstreamContinuation,
error,
task,
upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }

task.cancel()

downstreamContinuation.resume(throwing: error)
case let .cancelTaskAndUpstreamContinuations(
task,
upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }

task.cancel()
case .none:
break
}
group.cancelAll()
}

group.cancelAll()
}
}
}
Expand Down
48 changes: 24 additions & 24 deletions Sources/AsyncAlgorithms/Zip/ZipStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,31 +286,31 @@ final class ZipStorage<Base1: AsyncSequence, Base2: AsyncSequence, Base3: AsyncS
}
}

do {
try await group.waitForAll()
} catch {
// One of the upstream sequences threw an error
self.stateMachine.withCriticalRegion { stateMachine in
let action = stateMachine.upstreamThrew(error)

switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()

downstreamContinuation.resume(returning: .failure(error))

case .none:
break
}
}
while !group.isEmpty {
do {
try await group.next()
} catch {
// One of the upstream sequences threw an error
self.stateMachine.withCriticalRegion { stateMachine in
let action = stateMachine.upstreamThrew(error)
switch action {
case .resumeContinuationWithErrorAndCancelTaskAndUpstreamContinuations(
let downstreamContinuation,
let error,
let task,
let upstreamContinuations
):
upstreamContinuations.forEach { $0.resume(throwing: CancellationError()) }
task.cancel()

downstreamContinuation.resume(returning: .failure(error))
case .none:
break
}
}

group.cancelAll()
group.cancelAll()
}
}
}
}
Expand Down