Skip to content

[Docs][Concurrency] TaskGroup docs: waitForAll (non-)cancellation #63956

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
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
29 changes: 29 additions & 0 deletions stdlib/public/Concurrency/TaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,35 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
}

/// Wait for all of the group's remaining tasks to complete.
///
/// If any of the tasks throw, the *first* error thrown is captured
/// and re-thrown by this method although the task group is *not* cancelled
/// when this happens.
///
/// ### Cancelling the task group on first error
///
/// If you want to cancel the task group, and all "sibling" tasks,
/// whenever any of child tasks throws an error, use the following pattern instead:
///
/// ```
/// while !group.isEmpty {
/// do {
/// try await group.next()
/// } catch is CancellationError {
/// // we decide that cancellation errors thrown by children,
/// // should not cause cancellation of the entire group.
/// continue;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👽 ;

/// } catch {
/// // other errors though we print and cancel the group,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not really print, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks will add

/// // and all of the remaining child tasks within it.
/// group.cancelAll()
/// }
/// }
/// assert(group.isEmpty())
/// ```
///
/// - Throws: The *first* error that was thrown by a child task during draining all the tasks.
/// This first error is stored until all other tasks have completed, and is re-thrown afterwards.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only instance of waitForAll which has such special semantics.

Discarding groups don't have this API, nor can they use next(), and a not throwing one cannot throw so this cannot happen

@_alwaysEmitIntoClient
public mutating func waitForAll() async throws {
var firstError: Error? = nil
Expand Down