Yes, as cancellation is cooperative here, it’s the developers’ responsibility to property check for it and handle. So in case async function does not handle it or maybe already passed the point where cancellation was possible, or opposite — haven’t reached cancellation point yet.
As for immediate, cancellation almost never has been immediate. I mean, you will get actual task status in Task.isCancelled/Task.checkForCancellation, and withTaskCancellationHandler will allow to catch cancellation as soon as it has been invoked. Still that does not guarantee that work is stopped at this point right away.
In general, you better support cancellation whenever it is possible, especially with long-running and heavy tasks, so it is possible to avoid unnecessary work. Of course, it is still possible that some API doesn't provide proper cancellation. So I think not supporting cancellation should be rare case when you know this work should be completed.
More frequently, I guess, there will be the case when cancellation has been checked only at some points, so task will be executed until certain point after it has been cancelled:
func longRunning() async throws {
setup()
defer { cleanUp() }
await doSomeWork()
try Task.checkForCancellation()
await goFurther()
try Task.checkForCancellation()
finish()
}
func main() async throws {
async let _ = longRunning()
}
You can expect it to run untill the first cancellation check at this case. That means if, for example, setup and doSomeWork take, say, 10 seconds, the code will finish execution in 10s.
Probably a bit as it does not account for case with cancellation in overall discussion, yet the quoted equation is actually correct, time is variable property, e.g. if function ends earlier due to some exception, it will run less, or if it has conditional path in its body, and so on. So if you think of the time function in this equation as some non-determined function, output of which you cannot just imply from your code (which is true most of the time, as you'll rarely will have sleep in your code), it is pretty accurately describes what to expect.