Binding a task-local without a closure

Hi Swift users!

I want to talk about making task-locals more flexible while keeping them safe. The latest compiler changes forbid the unsafe way of binding them outside of withValue, but there is still no safe API that covers this use case. Here is the use case and an idea for such API.

I have a scope object that is created at the start of a function and ended at the end of it:

func handle(request: Request) async {
  let scope = Context.begin()
  defer { scope.end() }
  await process()   // everything called from here sees the context
}

Inside, the context is stored in a task-local, so that process() and child tasks can find it. That means the task-local has to be bound in begin() and unbound in end():

enum Context {
  @TaskLocal static var current: Store?

  static func begin() -> Scope {
    let store = Store()
    $current.push(store)   // bind for the rest of the frame
    return Scope(store)
  }
}

extension Scope {
  func end() {
    Context.$current.pop()
  }
}

I know about withValue, but I don't want it here. Every function that sets up a context would get one more level of indentation, and that is exactly what this code is trying to avoid.

So push and pop call the same runtime functions that withValue calls:

@_silgen_name("swift_task_localValuePush")
func _taskLocalValuePush(
  key: Builtin.RawPointer,
  value: __owned some Any
)

@_silgen_name("swift_task_localValuePop")
func _taskLocalValuePop()

extension TaskLocal {
  func push(_ value: __owned Value) {
    _taskLocalValuePush(key: key, value: consume value)
  }

  func pop() {
    _taskLocalValuePop()
  }
}

Push at the start, pop at the end, always balanced in the same frame (source). This worked fine up to 6.2. In 6.3 these two symbols were added to the compiler's list of runtime functions, and now there is a warning:

symbol name 'swift_task_localValuePush' is reserved for the Swift runtime
and cannot be directly referenced without causing unpredictable behavior;
this will become an error

The PR that added the warning says it is there to find out who uses these functions before it becomes an error. So here is one user :)

What I would really like is a binding that I can hold in a variable:

extension TaskLocal {
  struct Binding: ~Copyable { deinit { /* unbind */ } }
  func bind(_ value: Value) -> Binding
}

func handle(request: Request) async {
  let binding = $context.bind(Context())   // unbinds when `binding` dies
  await process()
}

SE-0311 mentions this in future directions ("language features to avoid nesting with withValue"). Back then the problem was that the task could outlive the binding. Now there are noncopyable types with deinit and nonescapable types, so it looks possible. And since the stack nesting fix in 6.3, withValue itself is built on a builtin that returns a binding token and another builtin that takes it back. That token is basically this struct. The hard part is probably the same as in that fix: the compiler has to know that init allocates on the task stack and deinit frees it, so its own swift_task_alloc calls nest correctly.

Is there any interest in this? I can write a pitch.

And meanwhile, is AllowRuntimeSymbolDeclarations the intended way to keep such code compiling, or will the error come without any alternative?

Thanks!

10 Likes

I do think your concern is valid, but I don't think we should ad-hoc solve it for one specific feature.

Instead, we should holistically solve all the with... methods, by replacing them with a form of

func x() {
  with let scope = thing // 

  // end of scope, triggers:
  // scope.end()
}

This idea has been floating around for a long time already and often referred to as context managers. It would help us get rid of this ever-growing problem of with... methods which we must use to keep APIs safe in structured concurrency contexts.

I would not want to spend significant effort on fixing one of the with methods ad-hoc and then we'll be left with the same issue still for with... cancellation handlers, priority escalation, task groups and everything else.

2 Likes