Skip to content

[SE-0505] Revise to support cancellation. - #3317

Open
al45tair wants to merge 12 commits into
swiftlang:mainfrom
al45tair:delayed-enqueuing-update
Open

al45tair wants to merge 12 commits into
swiftlang:mainfrom
al45tair:delayed-enqueuing-update

Conversation

@al45tair

@al45tair al45tair commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Updated the proposal to properly support cancellation (a corresponding implementation is available).

Updated the proposal to properly support cancellation (a corresponding
implementation is available).
@al45tair
al45tair force-pushed the delayed-enqueuing-update branch from a046a4e to d5cd7b2 Compare July 1, 2026 14:19
@rjmccall rjmccall added content change Content change to an existing document LSG Contains topics under the domain of the Language Steering Group labels Aug 18, 2026
  Revise the cancellation design to match the implementation.

  Cancellation:
  - `cancel` hands the job back (`ExecutorJob?`) rather than applying a
    `CancellationBehavior`, so the caller decides what to do with an
    unstarted job. Drop `CancellationBehavior` and the `onCancellation:`
    parameter, and say why in Alternatives Considered.
  - Split cancellation into `JobCanceller` (an `AnyObject, Sendable`
    protocol with `cancel(jobWithToken:)` and a defaulted
    `release(token:)`) and `JobCancellationToken`, which is `~Copyable`
    and `Sendable` and has a borrowing, idempotent `cancel()`.
    `release(token:)` is called exactly once, when the token is
    destroyed, including after a cancel. Replace the `@convention(thin)`
    clean-up function with it.
  - Store `opaqueData` as `InlineArray<2, Int>` instead of a tuple, and
    drop `jobID`.
  - Say that tokens are `@unsafe` to create and that an executor may only
    trust tokens it issued.
  - Add "Implementing `cancel`". Cancelling a `Task.sleep()` runs `cancel`
    from a cancellation handler while the runtime holds the task's status
    record lock, so `cancel` must not wait on anything that is held while
    jobs are enqueued or run. Describe the lock-free and try-lock
    approaches.

  Scheduling:
  - Rename `enqueue(_:run:...)` to `schedule(_:at:tolerance:clock:)`. Make
    `SchedulingExecutorFireTime` `Equatable`, `Hashable` and `Sendable`.
  - `supportsScheduling(on:)` has no default. Executors that don't
    handle a standard clock say so, and the runtime then never asks them
    to schedule it.
  - Add `SchedulingClock`, a permanent refining protocol of `Clock`, in
    place of the scheduling methods on `Clock`, and explain the choice.
    The standard clocks conform and schedule directly on a supporting
    executor.

  Task.sleep:
  - Describe `Task.sleep()` in terms of split continuations: the
    continuation is the job, so there is no separate continuation API or
    stand-in job. Explain why `cancel` returns the job rather than a
    continuation.
  - Describe how the executor is chosen, the fallback when there is no
    scheduling executor, and the allocation cost per sleep. Correct the
    earlier "no additional allocation" claim.

  Also update the ABI section to cover removed 6.3 SPI symbols, non-task
  jobs reaching the C deadline entry points (`swift_job_alloc` returns
  NULL for them), and the default Dispatch leeway change.
Removed the `asJob()` method in favour of providing a safe continuation
API.
Re-wrapped some of the text.

Tweaked the wording in places.

Added a note about the necessity of a Job-based API.
Comparing the proposal and the implementation, we need to update a comment
and also mention some of the other things that aren't there for Embedded
Swift.
I don't think we should do `async` methods for `sleep` or `withDeadline`;
this just pushes all of the implementation problems down into each
executor, where we should be able to handle many of the difficult parts
in the Concurrency runtime itself (making it much easier to write
executors).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

content change Content change to an existing document LSG Contains topics under the domain of the Language Steering Group

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants