-
Notifications
You must be signed in to change notification settings - Fork 61
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
Ability for coroutine to await its own cancellation #28
Comments
I agree with you that this is currently not directly supported on the public API. I don't know right this moment if this is feasible to support, I'll look into it for 2.0 or 2.1 and keep this issue open until a decision is made. Until then, you can either change your Outer to run in latent mode (which will make for (auto NestedCoro = Nested(); !NestedCoro.IsDone();)
co_await NextTick(); // This will repeatedly evaluate outer cancellation PlatformSeconds(AnyThread) should also work instead of NextTick if that better fits your real usage. |
Thanks for the tips, looking forward to seeing what 2.0 brings! |
This referred to async coroutines awaiting latent awaiters, which was improved in 2.0 to respond to cancellations within one tick, but it doesn't help here. Here, we have an async coroutine awaiting another async coroutine. |
Sorry for the delay. I've recently been able to test this in 2.0 preview 2, but I'm still unclear on how to have the Outer coroutine detect it's own cancellation immediately, rather than waiting until after Nested finishes. I've got the following test code, similar to before except added a new Manager coroutine to explicitly show how the cancellation is triggered.
Now without Manager cancelling Outer, it works as you'd expect - Outer finishes naturally after Nested, and the scope exits.
When the Manager does cancel Outer, it does prevent Outer resuming after Nested, and it does exit the scope and trigger the FOnCoroutineCanceled but it still only does so after Nested finishes.
Ideally, I'd have the scope exit and FOnCoroutineCanceled triggered immediately after the Cancel() call without having to wait for Nested to complete. What I'd expect in that case would be something like this:
The goal in this case being that Outer can execute some cleanup logic immediately upon cancellation, and possibly forward the cancellation onto other coroutines such as Nested in this case. |
You're right, I confused exactly what was in latent mode when I wrote my above comment. I edited it for clarity. Nothing on the current public API will serve you, but I've added Using |
Confirmed, that works great! Thanks for the new functionality and tips. |
Hey there and thanks for the amazing plugin.
I'm looking for some certain functionality regarding cancellation - in particular, having a coroutine respect its own cancellation while awaiting another nested coroutine.
I've read the cancellation documentation but can't find an example of this use case. Maybe this is already possible and I'm missing something.
Let's say I have a simple setup of one coroutine awaiting another:
And let's say that the Outer() coroutine is started and then has its Cancel() called while it's still running.
In this case, it seems the cancellation will not actually stop the outer coroutine before the nested one is complete.
Suppose I actually want the Outer coroutine to pick up its cancellation immediately and run some cleanup logic, before Nested() completes which may be a long time later. (Maybe forward that cancellation on to a nested coroutine to interrupt it earlier)
I feel like what I need is the ability to do something like this:
where WaitForCancellation() awaits and only resumes if Outer() is cancelled.
This way Outer() can handle its cancellation immediately.
I thought FinishNowIfCanceled() looked promising, however I need it to suspend if it's not cancelled rather than resuming immediately.
I also tried Latent::Until({ IsCurrentCoroutineCanceled(); }); but of course it gets evaluated outside of the context of the coroutine itself so can't get the cancellation status.
Does this make sense? Is it possible in the plugin already?
Thanks a lot.
The text was updated successfully, but these errors were encountered: