Skip to content

Update Task.cancel() with more details #76508

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 4 commits into from
Sep 18, 2024
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
43 changes: 34 additions & 9 deletions stdlib/public/Concurrency/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,44 @@ extension Task {
}
}

/// Indicates that the task should stop running.
/// Cancels this task.
///
/// Task cancellation is cooperative:
/// a task that supports cancellation
/// checks whether it has been canceled at various points during its work.
/// Cancelling a task has three primary effects:
///
/// Calling this method on a task that doesn't support cancellation
/// has no effect.
/// Likewise, if the task has already run
/// past the last point where it would stop early,
/// calling this method has no effect.
/// - It flags the task as cancelled.
/// - It causes any active cancellation handlers on the task to run (once).
/// - It cancels any child tasks and task groups of the task, including
/// those created in the future. If those tasks have cancellation handlers,
/// they also are triggered.
///
/// Task cancellation is cooperative and idempotent.
///
/// Cancelling a task does not automatically cause arbitrary functions on the task
/// to stop running or throw errors. A function _may_ choose to react
/// to cancellation by ending its work early, and it is conventional to
/// signal that to callers by throwing CancellationError. However,
/// a function that doesn't specifically check for cancellation will
/// run to completion normally even if the task it is running on is
/// cancelled. (Of course, it may still end early if it calls something
/// else that handles cancellation by throwing and then doesn't
/// handle the error.)
///
/// It is safe to cancel a task from any task or thread. It is safe for
/// multiple tasks or threads to cancel the same task at the same
/// time. Cancelling a task that has already been cancelled has no
/// additional effect.
///
/// `cancel` may need to acquire locks and synchronously run
/// arbitrary cancellation-handler code associated with the
/// cancelled task. To reduce the risk of deadlock, it is
/// recommended that callers release any locks they might be
/// holding before they call cancel.
///
/// If the task has already run past the last point where it could have
/// performed a cancellation check, cancelling it may have no observable effects.
///
/// - SeeAlso: `Task.checkCancellation()`
/// - SeeAlso: `withTaskCancellationHandler(operation:onCancel:isolation:)`
public func cancel() {
Builtin.cancelAsyncTask(_task)
}
Expand Down