-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Improve Task documentation. #85861
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
base: main
Are you sure you want to change the base?
Improve Task documentation. #85861
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,9 @@ import Swift | |
| /// you give up the ability | ||
| /// to wait for that task's result or cancel the task. | ||
| /// | ||
| /// To support operations on the current task, | ||
| /// which can be either a detached task or child task, | ||
| /// `Task` also exposes class methods like `yield()`. | ||
| /// To support operations on the current task, `Task` also exposes class methods | ||
| /// like `yield()`. The current task can be either an unstructured `Task`, a | ||
| /// detached unstructured `Task`, or a child task (an `async let` or a task group). | ||
| /// Because these methods are asynchronous, | ||
| /// they're always invoked as part of an existing task. | ||
| /// | ||
|
|
@@ -77,6 +77,38 @@ import Swift | |
| /// This reflects the fact that a task can be canceled for many reasons, | ||
| /// and additional reasons can accrue during the cancellation process. | ||
| /// | ||
| /// A `Task` becomes cancelled when its `cancel()` method is called on it. There | ||
| /// is no other mechanism by which a `Task` can become cancelled. This is true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats very sneaky wording I’m not comfortable with… its true for specifically an unstructured task but the wording here isn’t clear enough, it may sound like you mean “any task” |
||
| /// even of a `Task` instance which is created inside the operation of another | ||
| /// `Task`. You can manually propagate cancellation from an enclosing `Task` to | ||
| /// another task created during its operation via `withTaskCancellationHandler(operation:onCancel:isolation:)`: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should be double quotes to cause docc links
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think we should be giving complete examples here, we should link to the api which then has more examples |
||
| /// | ||
| /// ```swift | ||
| /// let outer = Task { | ||
| /// let inner = Task { | ||
| /// // The next statement will not throw an error unless cancellation of | ||
| /// // the `outer` task is manually propagated to the `inner` task via a | ||
| /// // cancellation handler: | ||
| /// try Task.checkCancellation() | ||
| /// return try await work() | ||
| /// } | ||
| /// return try await withTaskCancellationHandler { | ||
| /// try await inner.value | ||
| /// } onCancel: { | ||
| /// inner.cancel() | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// // When `outer` becomes cancelled, the `onCancel` handler above will run, | ||
| /// // causing `inner` to become cancelled, too. | ||
| /// outer.cancel() | ||
| /// ``` | ||
| /// | ||
| /// Any instance of `Task` that you initialize for yourself is a top-level, | ||
| /// _unstructured_ task. For more information about the implications of | ||
| /// unstructured tasks, read the ["Unstructured Concurrency"](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/#Unstructured-Concurrency) | ||
| /// portion of the Swift Programming Language book. | ||
| /// | ||
| /// ### Task closure lifetime | ||
| /// Tasks are initialized by passing a closure containing the code that will be executed by a given task. | ||
| /// | ||
|
|
@@ -200,9 +232,16 @@ extension Task { | |
| /// | ||
| /// - It flags the task as canceled. | ||
| /// - 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. | ||
| /// - It cancels any child tasks (lowercase-t tasks, like `async let` and | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not be calling things “lowercase tasks” that’s wording that’s not used by swift documentation anywhere |
||
| /// other forms of structured concurrency, not capital-t `Task` instances) | ||
| /// and task groups of the task, including those created in the future. If | ||
| /// those tasks have cancellation handlers, they also are triggered. | ||
| /// | ||
| /// Cancelling a `Task` does _not_ cancel other instances of `Task` that were | ||
| /// or will be created during its operation. Those other instances will not | ||
| /// become cancelled until and unless they are explicitly cancelled via | ||
| /// their `cancel()` methods being called on them. Read the "Task Cancellation" | ||
| /// section of the `Task` documentation for more information. | ||
| /// | ||
| /// Task cancellation is cooperative and idempotent. | ||
| /// | ||
|
|
@@ -671,8 +710,8 @@ extension Task where Success == Never, Failure == Never { | |
| /// and save it for long-term use. | ||
| /// To query the current task without saving a reference to it, | ||
| /// use properties like `currentPriority`. | ||
| /// If you need to store a reference to a task, | ||
| /// create an unstructured task using `Task.detached(priority:operation:)` instead. | ||
| /// If you need to store a reference to a task, create an unstructured task using | ||
| /// `Task.init(name:priority:operation:)` or `Task.detached(priority:operation:)` instead. | ||
| /// | ||
| /// - Parameters: | ||
| /// - body: A closure that takes an `UnsafeCurrentTask` parameter. | ||
|
|
||
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.
lazy question since i did not yet search myself, but is this "attached" terminology used anywhere else? i've personally never seen it AFAIK and it seems a bit odd. to what is the Task 'attached'?
Uh oh!
There was an error while loading. Please reload this page.
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.
I haven't seen it either. I have put it in quotes here (
"attached") to connote the fact that it isn't an accepted term but is being used in a casual manner for rhetorical purposes. I do think, however, that the existence ofTask.detachedimplies that an "attached" alternative must exist. I am also hoping here that by directly stating thatTask.initis the alternative, it should help clarify for the reader that there isn't, say, aTask.attachedsomeplace they've yet to stumble upon.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.
I would be happy to change
"attached"tonon-detachedif that would be preferable.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.
just my personal opinion, but both of those seem sort of 'overly jargony' and i'd be inclined to just avoid them if possible.
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.
We should not be introducing new terms like that. Please don’t call tasks “attached” that’s not a word we ever use anywhere.
these are called “unstructured tasks”.