Skip to content

Commit b025383

Browse files
committed
Integrate ktoso's changes without collapsing the task group APIs
1 parent 92fa9f3 commit b025383

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

proposals/0526-deadline.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,16 @@ If the closure were `@Sendable`, it couldn't access actor-isolated state like
255255
with surrounding code regardless of isolation context, while maintaining safety
256256
guarantees.
257257

258-
#### Cancellation
258+
#### Cancellation reasons
259259

260-
This API uses the base cancellation to communicate the expiration of the deadline.
261-
The information to differentiate a cancellation due to normal task cancellation is
262-
expanded to handle a new reason for cancellation:
263-
due to deadline expiration. Since this is not a closed set of possible reasons
260+
This proposal uses the cancellation mechanism to communicate the expiration of a deadline.
261+
262+
The cancellation error gains a new reason field which can be used to differentiate
263+
a cancellation due to normal task cancellation or due to deadline expiration.
264+
Since this is not a closed set of possible reasons,
264265
for future development, this reason is left as an open enumeration.
265266

266-
Today `CancellationError` is an empty type with no payload or information conveyed to indicate
267-
the reasoning for cancellation. [SE-0304](0304-structured-concurrency.md) originally noted that
267+
[SE-0304](0304-structured-concurrency.md) originally noted that
268268
"no information is passed to the task about why it was cancelled," treating cancellation as a
269269
lightweight, uniform signal. With the introduction of deadlines, however, differentiating between
270270
a cancellation due to deadline expiration and a cancellation from an explicit `Task.cancel()` call
@@ -346,7 +346,22 @@ public nonisolated(nonsending) func withTaskCancellationHandler<Return, Failure>
346346
This function works exactly as the existing `withTaskCancellationHandler` does today,
347347
except that the `onCancel` handler is passed the reason for cancellation.
348348

349-
Furthermore, the cancellation reason can be retrieved from the current Task similar to how cancellation is checked.
349+
Similar to how it is possible to query a task handle about its cancelled status using `isCancelled`,
350+
this proposal introduces a `cancellationReason` static property:
351+
352+
```swift
353+
extension Task where Success == Never, Failure == Never {
354+
/// Returns the reason of the cancellation if the current task is cancelled, nil otherwise.
355+
/// Similar to `isCancelled`, once this field becomes non-nil, consistently returns the same value.
356+
///
357+
/// - SeeAlso: `isCancelled`
358+
public static var cancellationReason: CancellationError.Reason? { get }
359+
}
360+
361+
extension UnsafeCurrentTask {
362+
public var cancellationReason: CancellationError.Reason? { get }
363+
}
364+
```
350365

351366
#### Failures and expiration
352367

0 commit comments

Comments
 (0)