-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cancellation #25
Comments
Hello! It seems like many people miss how cancellation works in structured concurrency. Therefore, I believe it's essential to know how to use cancellation properly. I noticed in Apple's example that cancellation wasn't correctly handled in the context of structured concurrency, which caused tasks not to be canceled properly. Is there a way I can contribute to addressing this issue? If you can guide me, I would be happy to prepare and contribute. :) If you check the last code in [Section2 - Step 8], even if the task running the quakeLocation asynchronous method is canceled, the task created inside the method is not canceled. This is because it is not using structured concurrency. To ensure cancellation as mentioned above, you need to wrap it with withTaskCancellationHandler. |
Thanks you for pointing this out! I was just looking at the tutorial example. Cancellation of caching behaviors is tricky! I'm including the code listing here just for reference: func quakeLocation(from url: URL) async throws -> QuakeLocation {
if let cached = quakeCache[url] {
switch cached {
case .ready(let location):
return location
case .inProgress(let task):
return try await task.value
}
}
let task = Task<QuakeLocation, Error> {
let data = try await downloader.httpData(from: url)
let location = try decoder.decode(QuakeLocation.self, from: data)
return location
}
quakeCache[url] = .inProgress(task)
do {
let location = try await task.value
quakeCache[url] = .ready(location)
return location
} catch {
quakeCache[url] = nil
throw error
}
} Consider this: A) client 1 calls At this point, if |
So I think we need to separate documentation around cancellation into two sections:
The above Quake example and your
The above is just some pseudocode but it dodges the |
@FranzBusch, this is great! Do you have any thoughts about how to discuss the complexity/functionality trade-offs here? Do you think the |
@FranzBusch When calling self.removeContinuation in the onCancel parameter, I encounter an "Call to actor-isolated instance method 'removeContinuation()' in a synchronous nonisolated context" error. Wrapping it with Task.init avoids the compile error, but that doesn't seem like the correct approach. Could you give me some more hints about this code?
|
I think we need to explain the underlying concepts first i.e. structuredness and cancellation. With good explanation around why structuredness is better. Then we can use this cache example as a more advanced setup of how to use structuredness.
Yes that's a correct error. The one way to work around this with |
I was starting to think a bit about cancellation and
withTaskCancellationHandler
. I'm not certain exactly which section this belongs in, but I think it is an important topic.The text was updated successfully, but these errors were encountered: