-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
Add more details into cancel() docs, some developers were worried if they are allowed to call it concurrently and multiple times etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's worth adding that any taskCancellationHandler that's setup is run once task.cancel() is called from the thread that calls it
I'm not sure we want to document the threading guarantee hmmm... I did include that a note that cancellation handlers run just once though, good enough? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed spelling of "canceled" per APSG and left a suggested rewording.
stdlib/public/Concurrency/Task.swift
Outdated
/// checks whether it has been canceled at various points during its work. | ||
/// checks whether it has been canceled at various points during its work, | ||
/// and may either return early or throw an error in order to | ||
/// finish running earlier than it would if it wasn't cancelled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Cancels this task.
Cancelling a task has three primary effects:
- It flags the task as cancelled.
- It causes any active cancellation handlers on the task to run.
- It cancels any child tasks and task groups of the task, including
those created in the future.Task cancellation is cooperative in the sense that 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 throwingCancellationError
. 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 callcancel
.
- SeeAlso:
Task.checkCancellation()
- SeeAlso:
withTaskCancellationHandler()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's pretty comprehensive, lemme take that -- thanks @rjmccall
Co-authored-by: Alex Martini <[email protected]>
Co-authored-by: Alex Martini <[email protected]>
@swift-ci please smoke test |
@amartini51 Please do a style check for the revised wording — we effectively have a sort of "merge conflict" here. |
Add more details into cancel() docs, some developers were worried if they are allowed to call it concurrently and multiple times etc.