SE-0564: Split Continuations

Hi everybody. The review of SE-0564, Split Continuations, begins now and runs through November 18, 2026.

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 or DM. When contacting the review manager directly, please put "SE-0564" 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 on GitHub.

Thank you for helping to improve Swift!

Joe Groff
Review Manager

10 Likes

A new withContinuation method passes both halves of a continuation to an async body.

Perhaps instead of overloading the existing method, the new method should have a new name, like withSplitContinuation, to avoid any potential type checker performance or behavioral regressions.

9 Likes

This seems like a necessary and correct extension, although I don't see any harm in following Slava's suggestion.

Personally, more than happy to avoid overloads :slight_smile: While we're not introducing a new "SplitContinuation" type, it is just the same Continuation and the waiter, but since this is an API reached for kinda rarely I think calling it this might be fine.

2 Likes

I'm not opposed to giving this a different name but I'm curious to understand the overhead this has on type checking. The two overloads have a different number of input parameters and we expect users to always use both. Is this still a problem?

I personally think this is underselling the new API and I expect that this will be the API reached by most developers over time. It is already understood as a good practice to set up cancellation handlers when creating continuations. As outlined in the motivation, that is non-trivial with the current ordering of handler and then continuation. The proposed API here makes this a lot easier so I expect developers to naturally gravitate to it.

3 Likes

My concerns are probably unfounded, but I want to note that unless I'm mistaken, the new overload has the same number of parameters as one of the previous ones, the difference is actually in the number of parameters of the function type of the last argument.

Here are the existing overloads:

public nonisolated(nonsending) func withContinuation<Success: ~Copyable, Failure: Error>(
  of: Success.Type = Success.self,
  throwing: Failure.Type,
  _ body: (consuming Continuation<Success, Failure>) -> Void
) async throws(Failure) -> sending Success {

public nonisolated(nonsending) func withContinuation<Success: ~Copyable>(
  of: Success.Type = Success.self,
  _ body: (consuming Continuation<Success, Never>) -> Void
) async -> sending Success {

Here is the new overload, compare it with the first existing overload above:

public nonisolated(nonsending) func withContinuation<
    Success: ~Copyable,
    Failure: Error,
    BodyResult: ~Copyable,
    BodyFailure: Error
>(
    of: Success.Type = Success.self,
    throwing: Failure.Type,
    _ body: nonisolated(nonsending) (
        consuming Continuation<Success, Failure>,
        consuming ContinuationWaiter<Success, Failure>
    ) async throws(BodyFailure) -> BodyResult
) async throws(BodyFailure) -> BodyResult

Coincidentally I'm currently working on a change to extend disjunction pruning to this case where overloads differ in their higher-order function arguments, but until that lands, adding such an overload can blow up performance on existing code that doesn't use the new overload.

6 Likes

Thanks for expanding.Yes I meant it is overloaded on the number of arguments of the last function argument. I am happy to follow any guidance on the naming that we believe is best with how type checking works currently and how it might improve in the future.

Somewhat unrelated, but I find myself using AsyncStream.makeStream() quite often as a more flexible continuation-like API. While this approach works perfectly fine, I wonder if there would be any interest in offering a similar API for Split Continuations.

1 Like

I'd be curious to hear why you find the AsyncStream API more flexible to use. Are you mostly using those for one-shot continuations that handle cancellation?

I mostly use this approach in tests. For example, we make extensive use of it in the Async{Throwing}Stream tests themselves, since it allows for more flexible setup: you get both pieces, the stream (waiter) and the continuation, without actually having to do any waiting at this point. (We often combine this approach with @MainActor for fine-grained execution control, though.)

I haven't made much use of AsyncStream's cancellation and termination behavior with this approach, but it could enable some interesting setups where, e.g., the stream acts as a condition-variable like mechanism.

What I mean is that in the grand scheme of things, not many developers reach for continuations of any kind; the existing ones or the new one both. Either way, I think it's fine if we do a more explicit name like Slava suggested, it helps naming the feature as well somewhat, otherwise folks may not know what we'd mean when we tell them to use "the split one" :slight_smile:

Just another thought.

I’ve always found it curious that the Continuation APIs use with-style functions, unlike withTaskCancellationHandler, withTaskCancellationShield, etc. The scope itself isn’t relevant here (obviously, this proposal would change that somewhat): the body of a with*Continuation API provides the continuation handle, which is then, in most cases, stored somewhere immediately. There is no “special behavior” while the body closure is executing.

1 Like

In the current withContinuation functions the scope is load-boarding since at the end of scope the task awaits the continuation. That's not the case here anymore but the with-style is still load-boarding to the task allocation ordering. Under the hood the split continuation API needs to allocate something using the task allocator which is strictly LIFO-based. The with-style guarantees us that we are ordering the freeing of the task allocations correctly.

This enhancement solves actual problems I came across in the past. Great! :+1:

One issue I have with the existing API remains, though. While I do understand the reasons for the scoped with-style APIs, both old and new, they introduce a potential suspension point before the continuation is passed to client code.

Though it is documented that the closure in withCheckedContinuation is executed before any actual suspension occurs, this fact is not visible in code. In reviews it looks like a reentrancy bug in actor isolated code that registers a continuation and must do so before any suspension.

While this is documented not to be a real problem it always bugs me and I find myself writing comments about the situation. I would prefer an API that let me create the continuation synchronously and do the waiting part (including the potental suspension point await) later.

When originally designing withContinuation, I was imagining that the task would always resume immediately (well, after a hop) when resume was called on the continuation. That was a design that required the initialization of the continuation object to be tied to a single awaiting point. However, it created a disastrous semantic problem where the “body” closure of withContinuation could run concurrently with the continuation of the task.

We then came up with the current behavior, where resume synchronizes with the end of the body, just atomically setting a flag if the end hasn’t been reached yet. We didn’t revisit all of the assumptions at the implementation level, though, and so initialization is still tied to a single awaiting point. This is unnecessary, and Franz’s proposal fixes it. So at this point, the API only really requires a scope for the allocation of the continuation object itself. (Even the dynamically-allocated CheckedContinuation is internally referencing an unsafe continuation object that’s allocated out of the async frame.) In theory, I suppose we could abandon that and just dynamically allocate the continuation, allowing the awaiter to be arbitrarily passed around on the original task. But I think delimiting the area where the continuation can be awaited is easier for people to understand, and the cheaper allocation is a nice plus.

2 Likes