# \[Pitch\] Task Naming API

**URL:** <https://forums.swift.org/t/pitch-task-naming-api/76115>\
**Category:** Pitches\
**Created:** [November 19, 2024, 3:10am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115 "2024-11-19T03:10:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![harjas](https://avatars.discourse-cdn.com/v4/letter/h/f19dbf/32.png) [@harjas](https://forums.swift.org/u/harjas)\
**Post date:** [November 19, 2024, 3:10am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/1 "2024-11-19T03:10:20Z")

</div>

Hi Swift Evolution,

The problem we are trying to solve with this pitch is that Swift Concurrency currently has no mechanism for labeling a Swift Task.

We propose adding APIs to allow for the creation of Swift Tasks with names. These names would be readable by a developer's own code and could be used by runtime-analysis tools, such as a debugger, in order to aid developers.

The proposal PR is up [here](https://github.com/swiftlang/swift-evolution/pull/2619).

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 19, 2024, 4:06am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/2 "2024-11-19T04:06:16Z")

</div>

> [@](#):
>
> In order to allow naming tasks, the following APIs will be provided on `Task`:
> 
> ```swift
> init(
> name: String? = nil,
> priority: TaskPriority? = nil,
> operation: sending @escaping @isolated(any) () async -> Success
> 
> static func detached(
> name: String? = nil,
> priority: TaskPriority? = nil,
> operation: sending @escaping @isolated(any) () async -> Success
> 
> ```

Are the older "nameless" versions of these APIs going to be deprecated (or made ABI-only entrypoints)?

I _think_ there are corner cases where two non-deprecated APIs that differ only in having or not having an extra defaulted argument becomes ambiguous. Plus, it'd be API surface area that we wouldn't have if we were starting from the beginning.

If, on the other hand, there are important reasons why the "nameless" versions need to continue on as first-class API, then I think these should not have a default value for `name` (such that omitting it always means that the "nameless" version is invoked unambiguously).

* * *

> [@](#):
>
> In addition to that, it will be possible to read a name off a task, similar to how the current task's priority is possible to be read:
> 
> ```swift
> extension Task {
> var currentName: String? { get } 
> }
> 
> ```

"Current name" implies that there could be other, different past or future names, but as far as I can tell the task name cannot be modified, and certainly not through this get-only property?

---

<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:** [November 19, 2024, 4:11am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/3 "2024-11-19T04:11:14Z")

</div>

> [@xwu](#):
>
> I _think_ there are corner cases where two non-deprecated APIs that differ only in having or not having an extra defaulted argument becomes ambiguous. Plus, it'd be API surface area that we wouldn't have if we were starting from the beginning.

For what it's worth we've already had these situations in the past and we kept both declarations so far. When we added `taskExecutor` we added the new one, with "new" availability.

```swift
  @available(SwiftStdlib 6.0, *)
  @discardableResult
  @_alwaysEmitIntoClient
  public static func detached(
    executorPreference taskExecutor: (any TaskExecutor)?,
    priority: TaskPriority? = nil,
    operation: sending @escaping () async -> Success
  ) -> Task<Success, Failure>

```

We didn't do anything special for them, nor did we remove the old overloads. I think we'd probably do the same here.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 19, 2024, 4:13am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/4 "2024-11-19T04:13:14Z")

</div>

> [@ktoso](#):
>
> When we added `taskExecutor` we added the new one, with "new" availability.

This example (if I'm not missing something) shows `executorPreference` _without_ a default value. My specific feedback here is that an overload with additional argument _that is defaulted_ may pose problems with ambiguity.

---

<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:** [November 19, 2024, 4:13am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/5 "2024-11-19T04:13:36Z")

</div>

> [@xwu](#):
>
> > [@](#):
> >
> > ```swift
> > extension Task {
> > var currentName: String? { get } 
> > }
> > 
> > ```
> 
> "Current name" implies that there could be other, different past or future names, but as far as I can tell the task name cannot be modified, and certainly not through this get-only property?

This is following existing naming convention on Task APIs:

```swift
var currentPriority: TaskPriority 
public func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) throws -> T) rethrows -> T

```

where "current" means "take it from the executing (current) context".

Although this convention was broken by `basePriority` but the introduction of that property had some process issues actually... I'd prefer to stick to the "current" meaning, but it's up for discussion of course.

It's also commonly used in `Thing.current` in task-local variables.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 19, 2024, 4:14am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/6 "2024-11-19T04:14:39Z")

</div>

> [@ktoso](#):
>
> This is following existing naming convention on Task APIs:
> 
> ```swift
> var currentPriority: TaskPriority 
> public func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) throws -> T) rethrows -> T
> 
> ```
> 
> where "current" means "take it from the executing (current) context".

In "current task," the adjective modifies "task"; in "current name," the adjective modifies "name."

Unless I'm mistaken, the proposed API is an _instance_ property on a specific task, whether current or not. If I have an instance of type `Task`, can it have a past or future name?

---

<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:** [November 19, 2024, 4:14am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/7 "2024-11-19T04:14:56Z")

</div>

I see, yes that's a good point -- the proposal should be clear that we're adding an overload without a default value which hopefully would not conflict...

It's quite tricky as we keep adding more overloads of those APIs but we'll have to figure it out like that.

---

<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:** [November 19, 2024, 4:16am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/8 "2024-11-19T04:16:11Z")

</div>

> [@xwu](#):
>
> Unless I'm mistaken, the proposed API is an _instance_ property on a specific task, whether current or not. If I have an instance of type `Task`, can it have a past or future name?

I see the confusion.

The proposal needs to be more clear here; if we're to follow existing terminology it would be:

```swift
static var currentName: String? { get } 
var name: String? { get } 

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 19, 2024, 4:16am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/9 "2024-11-19T04:16:20Z")

</div>

> [@ktoso](#):
>
> the proposal should be clear that we're adding an overload without a default value which hopefully would not conflict...

In that case, I think it's fine 🙂

The pitched design _does_ (explicitly!) have a default value on all the proposed `name` arguments, though.

---

<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:** [November 19, 2024, 4:16am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/10 "2024-11-19T04:16:50Z")

</div>

Thanks for the review, let me cleanup some of that.

// edit: I proposed some of the fixups into @harjas 's PR 👍

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 19, 2024, 4:17am UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/11 "2024-11-19T04:17:01Z")

</div>

> [@ktoso](#):
>
> The proposal needs to be more clear here; if we're to follow existing terminology it would be:
> 
> ```swift
> static var currentName: String? { get } 
> var name: String? { get } 
> 
> ```

That makes sense to me!

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [November 20, 2024, 2:42pm UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/12 "2024-11-20T14:42:15Z")

</div>

I am very much in support of this proposal. While the motivation calls out its usefulness in debugging my use-case is around testing. With the introduction of `TaskExecutor`s it is almost possible to write a deterministic executor that can be used for testing small isolated pieces of code that depend on a precise execution order of jobs. Below is a prototype of such an executor using task IDs for defining the order.

> **TestExecutor**
>
> ```swift
> import Dispatch
> 
> final class TestExecutor: TaskExecutor, @unchecked Sendable {
> private let queue = DispatchSerialQueue(label: "queue")
> 
> var order = [UInt64]()
> var jobs = [UInt64: UnownedJob]()
> 
> init(taskIDOrder: [UInt64]) {
> self.order = Array(taskIDOrder.reversed())
> }
> 
> func enqueue(_ job: consuming ExecutorJob) {
> let unownedJob = UnownedJob(job)
> let taskID = _getJobTaskId(unownedJob)
> print("Enqueue job \(taskID)")
> queue.async {
> if self.order.last == taskID {
> print("Running job \(taskID)")
> self.order.removeLast()
> unownedJob.runSynchronously(on: self.asUnownedTaskExecutor())
> print("Done running job \(taskID)")
> 
> while let nextJobId = self.order.last, let nextJob = self.jobs[nextJobId] {
> self.order.removeLast()
> self.jobs.removeValue(forKey: nextJobId)
> 
> print("Running job \(nextJobId)")
> nextJob.runSynchronously(on: self.asUnownedTaskExecutor())
> print("Done running job \(taskID)")
> }
> } else {
> print("Queuing job \(taskID)")
> self.jobs[taskID] = unownedJob
> }
> }
> }
> }
> 
> @_silgen_name("swift_task_getJobTaskId")
> internal func _getJobTaskId(_ job: UnownedJob) -> UInt64
> 
> ```

Now the problem with that is that task IDs are monotonically increasing throughout the lifetime of a process making them really unfit to derive an order. However, the proposed task names here would be a great fit. The only thing that is missing in this proposal is to add APIs to `Unowned/ExecutorJob` to query for the task name of the job.

---

<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:** [March 10, 2025, 12:32pm UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/13 "2025-03-10T12:32:54Z")

</div>

Hi folks,  
while this is waiting for a review thread to kick off I did add one more thing to the proposal: [Swift Task Naming API by Harjas12 · Pull Request #2619 · swiftlang/swift-evolution · GitHub](https://github.com/swiftlang/swift-evolution/pull/2619/commits/5be0eeb6b7f1ef5436f5444dad2f8e305f73f050)

We're proposing adding an `UnsafeCurrentTask?` returning accessor, in order for an `Executor` to be able to check out the task's name; it can be used in logging or other things.

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [March 13, 2025, 10:18pm UTC](https://forums.swift.org/t/pitch-task-naming-api/76115/14 "2025-03-13T22:18:08Z")

</div>

Thanks everyone for the pitch discussion. This is now in review here:

> [@SE-0469: Task Naming](https://forums.swift.org/t/se-0469-task-naming/78509):
>
> Hello, Swift community! The review of [SE-0469: Task Naming](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0469-task-names.md) begins now and runs through March 27th, 2025. 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 me as the review manager by [email](mailto:hborla@apple.com) or [DM](https://forums.swift.org/u/hborla/summary). When contacting the review manager directly, please put "SE-0469" in the subject line. Trying it out If you'd like to try this proposal out, you can download a toolch…

Please take any further feedback over to the review thread!
