-
What does this function actually do? What does it do the memory the closure has ownership of? Is it a bad practice? If it is a bad practice what is a better way to stop a running rask? Does it cause UB? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The Cancellation is done by running the destructor of the future, which runs the destructor of all local variables defined inside the future. This also cleans up all memory owned by the future. Using It certainly does not cause UB. Like the standard library, Tokio promises that causing UB with only safe code is impossible. Since you can call |
Beta Was this translation helpful? Give feedback.
-
@itamarsch If the docs are lacking, we would appreciate a PR to improve them. You can try to expand on them using what you learned here. |
Beta Was this translation helpful? Give feedback.
-
Sure!
…On Mon, Dec 4, 2023 at 4:03 AM Alice Ryhl ***@***.***> wrote:
@ShaneMurphy2 <https://github.com/ShaneMurphy2> Would you be willing to
open a PR to add it? Or open a documentation bug saying that it should be
added?
—
Reply to this email directly, view it on GitHub
<#5534 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AU7ZBQGIBDK74MBAOXXUVC3YHW3XVAVCNFSM6AAAAAAVUI3CM6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TONJSGM2TG>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
The
abort
method will cancel the task next time it yields at an.await
. For tasks that don't block the thread, this means that it will be cancelled immediately.Cancellation is done by running the destructor of the future, which runs the destructor of all local variables defined inside the future. This also cleans up all memory owned by the future.
Using
abort
is not bad practice — it works great as long as you don't need to do any asynchronous cleanup. For cases whereabort
is not sufficient, you can use the techniques outlined in the page on graceful shutdown on the Tokio website.It certainly does not cause UB. Like the standard library, Tokio promises that causing UB with only safe co…