Tuesday, October 14, 2025

Cultivated Task Cancellation

Max Seelemann (on his new blog):

So how do you tell if a task supports cancellation? That’s tricky to answer per-se because cancellation is voluntary behavior and needs to be represented in the task’s value or error type. If you’re lucky, cancellation support (or lack thereof) is documented.

[…]

The designated way to check for cancellation is to use static properties like Task.isCancelled. Using static properties may seem odd at first, but it’s clever: APIs called inside the task can check for cancellation without knowing where they’re running or taking task handles as arguments. In fact, such APIs are probably the best way to handle cancellation, as we’ll see shortly.

[…]

You might be thinking, “okay, nice and all. But why go to all this effort to explain something that’s baked into the system anyway?” Well, I fear here comes an inconvenient truth. Not many things in the Standard Library and Foundation have cancellation support built in. In fact, as far as I know, it’s just three.

Comments RSS · Twitter · Mastodon

Leave a Comment