# SE-0304 (3rd review): Structured Concurrency

**URL:** <https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847>\
**Category:** Proposal Reviews\
**Created:** [May 24, 2021, 10:52pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847 "2021-05-24T22:52:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [May 24, 2021, 10:52pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/1 "2021-05-24T22:52:14Z")

</div>

Hello, Swift community.

The third review of [SE-0304: Structured Concurrency](https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md) begins now and runs through June 3rd, 2021.

Following on from the [second review](https://forums.swift.org/t/se-0304-2nd-review-structured-concurrency/47217), the proposal authors have made a number of changes. The changes made after the second review can be found at the [end of the proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md#review-changes), and the full diff can be found [here](https://github.com/apple/swift-evolution/commit/01bdbdc2be26f9f26e4fad97e89d6648be1a6917#diff-6e3f26a7c1e2c41a13bcf34ef4c7d84625339b2898702f5e0bed0d6e05f1a778).

Note that this feature is closely tied to the [async let proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0317-async-let.md), the [first review](https://forums.swift.org/t/se-0317-async-let/48848) of which is happening concurrently in order to better discuss common aspects such as naming.

This review is part of the large [concurrency feature](https://forums.swift.org/t/swift-concurrency-roadmap/41611), which we are reviewing in several parts. While we've tried to make it independent of other concurrency proposals that have not yet been reviewed, it may have some dependencies that we've failed to eliminate. Please do your best to review it on its own merits, while still understanding its relationship to the larger feature. You may also want to raise interactions with previous already-accepted proposals – that might lead to opening up a separate thread of further discussion to keep the review focused.

Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to keep your feedback private, directly to the [review manager](mailto:ben_cohen@apple.com). If you do email me directly, please put "SE-0302" somewhere in the subject line.

## What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

- What is your evaluation of the proposal?
- Is the problem being addressed significant enough to warrant a change to Swift?
- Does this proposal fit well with the feel and direction of Swift?
- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
- How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at:

> [https://github.com/apple/swift-evolution/blob/master/process.md](https://github.com/apple/swift-evolution/blob/master/process.md)

As always, thank you for contributing to Swift.

Ben Cohen  
Review Manager

---

<div class="post-metadata">

**Author:** ![Alejandro\_Martinez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro_martinez/32/821_2.png) [@Alejandro\_Martinez](https://forums.swift.org/u/Alejandro_Martinez)\
**Post date:** [May 25, 2021, 8:00am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/2 "2021-05-25T08:00:51Z")

</div>

I was already very positive about the previous iterations and these last changes made the whole API more consistent in terms of naming. The proposal is also clearer now in terms of documenting which different kind of tasks exists (child, unstructured, detached) which I appreciate a lot.

Overall big +1

---

<div class="post-metadata">

**Author:** ![nikitamounier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikitamounier/32/24825_2.png) [@nikitamounier](https://forums.swift.org/u/nikitamounier)\
**Post date:** [May 25, 2021, 8:10am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/3 "2021-05-25T08:10:41Z")

</div>

I always thought from the get-go that a discardable initialiser with a closure would be the most natural way to create unstructured tasks. I also appreciate the platform-independent naming of the task priorities. This is definitely the best form of the Task API yet.

Big +1

---

<div class="post-metadata">

**Author:** ![s-k](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/s-k/32/6796_2.png) [@s-k](https://forums.swift.org/u/s-k)\
**Post date:** [May 25, 2021, 4:43pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/4 "2021-05-25T16:43:30Z")

</div>

Overall, I like this proposal very much.

I have a question: In the section [Cancellation handlers](https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md#cancellation-handlers), it says:

> If the task has already been cancelled at the point `withTaskCancellationHandler` is called, the cancellation handler is invoked immediately, before the `operation` block is executed.

Wouldn't this mean that the following code from the proposal would complete the download and not cancel it if the task had already been cancelled at the start of the function?

```swift
func download(url: URL) async throws -> Data? {
  var urlSessionTask: URLSessionTask?

  return try withTaskCancellationHandler {
    return try await withUnsafeThrowingContinuation { continuation in
      urlSessionTask = URLSession.shared.dataTask(with: url) { data, _, error in
        if let error = error {
          // Ideally translate NSURLErrorCancelled to CancellationError here
          continuation.resume(throwing: error)
        } else {
          continuation.resume(returning: data)
        }
      }
      urlSessionTask?.resume()
    }
  }, onCancel: {
    urlSessionTask?.cancel() // runs immediately when cancelled
  }
}

```

---

<div class="post-metadata">

**Author:** ![s-k](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/s-k/32/6796_2.png) [@s-k](https://forums.swift.org/u/s-k)\
**Post date:** [May 25, 2021, 5:12pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/5 "2021-05-25T17:12:00Z")

</div>

And I have a more serious concern:

```swift
func gather(first m: Int, of work: [Work]) async throws -> [WorkResult] { 
  assert(m <= work.count) 
  
  return withTaskGroup(of: WorkResult.self) { group in 
    for w in work { 
      group.async { await w.doIt() } // spawn child tasks to perform the work
    }  
    
    var results: [WorkResult] = []
    while results.count <= m { 
      switch try await group.nextResult() { 
      case nil: return results
      case .success(let r): results.append(r)
      case .failure(let e): print("Ignore error: \(e)")
      }
    }
  }
}

```

In this code snippet from the proposal, the `group.cancelAll()` (in addition to the `return results`) has been mistakenly left out. During the history of this proposal, this exact mistake has been repeatedly made (at least three times) by the proposal authors as well as other members of this forum. Forgetting the `group.cancelAll()` would have no effect on the behavior of the code, only on its performance. Thus, it is a very hard to find bug.

I am a firm believer that good APIs must make it hard to make mistakes. Therefore, I think it should be an explicit choice for the programmer if the remaining tasks shall be cancelled or not.

(Obviously, in this case, the missing return statement would generate a compiler error. However, it is possible and maybe even likely that this would result in the programmer only adding the return statement to satisfy the compiler and still forgetting the `group.cancelAll()`.)

> **One possible solution**
>
> Changing the return type of `operation` to an enum so that the return statement would either read `return .cancellingRemainingTasks(result)` or `return .finishingRemainingTasks(result)` would be ideal in my opinion, because it would virtually eliminate mistakes as seen above. Maybe there are less verbose options that achieve the same goal.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 25, 2021, 10:15pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/6 "2021-05-25T22:15:31Z")

</div>

> [@s-k](#):
>
> > If the task has already been cancelled at the point `withTaskCancellationHandler` is called, the cancellation handler is invoked immediately, before the `operation` block is executed.
> 
> Wouldn't this mean that the following code from the proposal would complete the download and not cancel it if the task had already been cancelled at the start of the function?

I don't understand your concern here.

It is exactly phrased this way to support immediately cancelling the work:

1. task is cancelled
2. task attempts to start the session task, does so
3. onCancel runs immediately, cancelling the urlSessionTask

You could write this in other ways too: prepare the url task, check for cancellation (and/or store the cancellation handler), only resume the task if not cancelled etc.

Better cancellation aware URLSession APIs may appear in the future as well, so that'd handle that transparently perhaps(?).

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [May 25, 2021, 11:02pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/7 "2021-05-25T23:02:15Z")

</div>

Could you be more explicit here? I think the confusion is around the order of operations within the nested cancellation handler and continuation. A simple reading line-by-line would seem to have the cancellation handler called before the task is even created, leaving the continuation to create and resume the task after cancellation.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 26, 2021, 3:41am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/8 "2021-05-26T03:41:02Z")

</div>

I just want to confirm. What would happen in task group when a spawned task finishes after the group is cancelled, do they still get added to the `group`'s results and are accessible via `next`?

It seems to be the case given that we allow tasks to ignore cancellation by design, but I'm not too sure.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 26, 2021, 3:42am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/9 "2021-05-26T03:42:26Z")

</div>

> [@Lantua](#):
>
> I just want to confirm. What would happen in task group when a spawned task finishes after the group is cancelled, do they still get added to the `group` 's results and are accessible via `next` ?

Cancellation that happens after a task completed is not going to impact the task or its result in any way.

---

<div class="post-metadata">

**Author:** ![s-k](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/s-k/32/6796_2.png) [@s-k](https://forums.swift.org/u/s-k)\
**Post date:** [May 26, 2021, 6:05am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/10 "2021-05-26T06:05:44Z")

</div>

> [@ktoso](#):
>
> I don't understand your concern here.

Thank you for your response. My concern is the following:

To quote from the proposal:

> If the task has already been cancelled at the point `withTaskCancellationHandler` is called, the cancellation handler is invoked immediately, before the `operation` block is executed.

To me, this reads like the cancellation handler is called **before** the `operation` block if the task is already cancelled. In the NSURLSession example, this would mean that `urlSessionTask?.cancel()` is called before `urlSessionTask` is set, making it a no-op. After that, the `operation` block would create and resume the data task so that it would complete without being cancelled.

Is this correct or have I misunderstood the proposal?

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 26, 2021, 6:10am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/11 "2021-05-26T06:10:57Z")

</div>

I see, thanks — I’ll double check but suspect the proposal wording may be wrong here. I have a radar on myself to ensure cancellations always will do the right thing in such situations.

Might be that we retained old wording in the proposal 🤔

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [May 26, 2021, 11:31am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/12 "2021-05-26T11:31:51Z")

</div>

Sorry if it's not clear, I mean the case where the cancellation happens when the task group and some spawn tasks are still running, i.e.

1. Task Group (A) spawns a child task (B),
2. Task A is cancelled (and so is Task B),
3. Task B completes normally, ignoring the cancellation,
4. [here] What is returned if I do `group.next()` right here?

I think someone could mistakenly think that `group.next()` would return `nil` instead of Task B's result (and vice versa) here.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 26, 2021, 11:51am UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/13 "2021-05-26T11:51:46Z")

</div>

It will return the result; Nowhere is it implied that a result added to the group is magically dropped.

Tasks always return a result; they may throw or return some “other” result if they notice cancellation but that’s about it.

---

<div class="post-metadata">

**Author:** ![benlings](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benlings/32/13011_2.png) [@benlings](https://forums.swift.org/u/benlings)\
**Post date:** [May 26, 2021, 9:01pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/14 "2021-05-26T21:01:42Z")

</div>

I’m glad the Apple-specific names for priorities have been retained as aliases - I’ve found they help clarify when to use a particular level.

Is there any difference between `yield()` and `sleep(0)`? This is a [question that comes up for other platforms](https://stackoverflow.com/questions/2956961/difference-between-thread-sleep0-and-thread-yield) and would be good to clarify.

What’s the behaviour of `sleep()` if the task is cancelled? Does it wait for the entire duration, or return early? If the former, would it be possible to have an equivalent to that will return/throw early on cancellation? (I know this was mentioned in earlier threads but couldn’t find a response)

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [May 26, 2021, 9:47pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/15 "2021-05-26T21:47:13Z")

</div>

> [@benlings](#):
>
> What’s the behaviour of `sleep()` if the task is cancelled? Does it wait for the entire duration, or return early?

That's a great point! If you are ignoring cancellation, you definitely do **NOT** want `sleep()` to end early, and if you want to react as fast as possible on cancellation you definitely **DO** want `sleep()` to end early.

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 1, 2021, 2:55pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/16 "2021-06-01T14:55:38Z")

</div>

**Task Groups**

It should be possible to have a single `TaskGroup<Success, Failure: Error>` type for throwing and non-throwing subtasks — if the group doesn't conform directly to `AsyncSequence`, but instead has different `values` properties for `Failure == Error` and `Failure == Never`.

However, to avoid ambiguity you'd still need different top-level `with{Throwing}TaskGroup` functions, so I'm not sure if this is worth changing. Unless you instead have `with...` static methods for `Failure == Error` and `Failure == Never`.

UPDATE: Should the `asyncUnlessCancelled` methods have `@discardableResult`s?

* * *

**Task Priorities**

For the platform-independent priorities:

- `.high` and `.low` are fine,

- `.default` could be renamed to `.medium` (to make the relative ordering more obvious),

- `.background` might be removed, if it can be implied (e.g. in a daemon background process, or when using the [`BackgroundTasks`](https://developer.apple.com/documentation/backgroundtasks) framework).

The other aliases will impair auto-completion. Can the Dispatch and Foundation frameworks extend `TaskPriority` with initializers taking QoS parameters?

* * *

**Voluntary Suspension**

> The sleep function accepts a plain integer as nanoseconds to sleep for which mirrors known top-level functions performing the same action in the synchronous world.

[`Darwin.sleep(_:)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html) has a `seconds: CUnsignedInt` parameter. Which top-level functions accept a plain integer as _nanoseconds_?

`Task.sleep(_:)` would be improved with an argument label, and possibly a different parameter type:

```swift
extension Task where Success == Never, Failure == Never {
  public static func sleep(forTimeInterval seconds: Double) async
}

```

This would match the existing [`Foundation.Thread`](https://developer.apple.com/documentation/foundation/thread/1413673-sleep) API, and be compatible with the `Foundation.Date` APIs. If `Double` (as an alias for `TimeInterval`) has 15 significant digits, then it should have nanosecond precision for a range of several days?

> Because use-sites look quite explicit in the way they have to prefix this call with an `await` keyword (`await Task.sleep(nanos)`), we prefer to use the well-known `sleep` word rather than introduce new words for this functionality.

`Task.yield()` could also be renamed. The documentation comments for both APIs are "suspends the current task", so perhaps `await Task.suspend()` and `await Task.suspend(forTimeInterval: seconds)`.

Can only the current task be suspended, or should it be possible to suspend (and resume) via a task handle?

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [June 1, 2021, 6:28pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/17 "2021-06-01T18:28:56Z")

</div>

> [@benrimmington](#):
>
> [`Darwin.sleep(_:)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html) has a `seconds: CUnsignedInt` parameter. Which top-level functions accept a plain integer as _nanoseconds_ ?
> 
> `Task.sleep(_:)` would be improved with an argument label, and possibly a different parameter type:
> 
> ```swift
> extension Task where Success == Never, Failure == Never {
> public static func sleep(forTimeInterval seconds: Double) async
> }
> 
> ```
> 
> This would match the existing [`Foundation.Thread`](https://developer.apple.com/documentation/foundation/thread/1413673-sleep) API, and be compatible with the `Foundation.Date` APIs. If `Double` (as an alias for `TimeInterval` ) has 15 significant digits, then it should have nanosecond precision for a range of several days?

That `sleep` has an unlabeled parameter specified in nanoseconds is definitely a problem. I'm not sure we want to push the idea of specifying seconds as a `Double` down into the standard library at all, and doing so for this one API seems unfortunate. Perhaps the easy way out is to use an argument label:

```swift
extension Task where Success == Never, Failure == Never {
  public static func sleep(nanoseconds: UInt64) async
}

```

The API is intentionally ugly, and leaves open space for us to create a nicer `sleep(_:)` with a proper time type at some point in the future.

> [@benrimmington](#):
>
> > Because use-sites look quite explicit in the way they have to prefix this call with an `await` keyword ( `await Task.sleep(nanos)` ), we prefer to use the well-known `sleep` word rather than introduce new words for this functionality.
> 
> `Task.yield()` could also be renamed. The documentation comments for both APIs are "suspends the current task", so perhaps `await Task.suspend()` and `await Task.suspend(forTimeInterval: seconds)` .

Yeah. The [AsyncStream proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0314-async-stream.md) and [modify accessors](https://forums.swift.org/t/modify-accessors/31872) consistently use "yield" to mean "produce a value". I agree that `suspend` is a better term here.

> [@benrimmington](#):
>
> Can only the current task be suspended, or should it be possible to suspend (and resume) via a task handle?

Only the current task. You cannot ask someone else's task to suspend, because they might not be at a potential suspension point.

Doug

---

<div class="post-metadata">

**Author:** ![benlings](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benlings/32/13011_2.png) [@benlings](https://forums.swift.org/u/benlings)\
**Post date:** [June 1, 2021, 7:30pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/18 "2021-06-01T19:30:38Z")

</div>

Would combining both into a single API be an option?

```swift
extension Task where Success == Never, Failure == Never {
  public static func suspend(nanoseconds: UInt64 = 0) async
}

```

Or is there a difference between `yield()` and `sleep(0)`?

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [June 1, 2021, 9:30pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/19 "2021-06-01T21:30:51Z")

</div>

> [@Douglas\_Gregor](#):
>
> ```swift
> extension Task where Success == Never, Failure == Never {
> public static func sleep(nanoseconds: UInt64) async
> }
> 
> ```
> 
> The API is intentionally ugly, and leaves open space for us to create a nicer `sleep(_:)` with a proper time type at some point in the future.

That sounds good to me, yeah.

> [@Douglas\_Gregor](#):
>
> Yeah. The [AsyncStream proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0314-async-stream.md) and [modify accessors](https://forums.swift.org/t/modify-accessors/31872) consistently use "yield" to mean "produce a value". I agree that `suspend` is a better term here.

To be honest I, personally, disagree with the yield used in the AsyncStream proposal (in which the type itself is also IMHO misnamed).

We can name the operation here `suspend` if we want to, but not because the stream source type uses yield -- as that type actually misuses it IMHO (I don't think a `yield` should be allowed to fail, which is the core idea of the proposed there API).

---

<div class="post-metadata">

**Author:** ![benlings](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benlings/32/13011_2.png) [@benlings](https://forums.swift.org/u/benlings)\
**Post date:** [June 1, 2021, 9:41pm UTC](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847/20 "2021-06-01T21:41:48Z")

</div>

I had a dig into the Task and Dispatch source and it looks like `yield()` is approximately `dispatch_apply_f` and `sleep` is `dispatch_after_f`. `dispatch_after_f` calls `dispatch_apply_f` if `when == DISPATCH_TIME_NOW`, so `sleep(0)` would be just a slightly less efficient `yield()`.

[Next page](https://forums.swift.org/t/se-0304-3rd-review-structured-concurrency/48847.md?page=2)
