I've recently been trying out new things with TaskGroup
s but I've hit a point with child task's priority argument where I am not sure if it is a bug or just something that is to be expected.
Consider this example:
Task(priority: .high) {
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask(priority: .low) {
print("child", "base", Task.basePriority!, "current", Task.currentPriority)
}
print("parent", "base", Task.basePriority!, "current", Task.currentPriority)
}
}
This is the output:
parent base TaskPriority.high current TaskPriority.high
child base TaskPriority.low current TaskPriority.high
Even though the child tasks priority was set to .low
it will print .high
as its current priority.
A couple of thoughts that I had what could explain this:
- This is due to privilege escalation similar to
Task.value
. However it is not. If you switch the priorities, the child task will still inherit the parent's task priority, but then it would be de-escalation (?). Task.currentPriority
simply ignores the priority of a task groups child task. This would be weird, becauseTask.basePriority
returns the correct base priority.
It feels like the parent Task
's priority will always override the priority of the child task. Am I missing something?
Thanks.