cunarist / tokio-with-wasm

Mimicking tokio functionalies on web browsers
MIT License
31 stars 2 forks source link

Support `abort()` for `JoinHandle<T>` #8

Closed namse closed 1 month ago

namse commented 1 month ago

Is it possible to impl abort() fn for JoinHandle<T>? I need this feature to make a compatibility with real tokio.

I have a idea that make a atomic boolean variable in JoinHandle and cancel in poll function, but I'm not sure it is enough.

temeddix commented 1 month ago

If I understood correctly, this would be an issue about catching panics.

Unfortunately, unwinding the stack upon panic doesn't work on wasm32-unknown-unknown. Therefore, we cannot expect JoinHandle to return Err when the spawned task has panicked. This is limitation we cannot really solve.

I'll leave a link to a detailed description:

namse commented 1 month ago

It's not about catching panics. It's about cancellation of Future (or Poll).

https://blog.yoshuawuyts.com/async-cancellation-1/ or https://theincredibleholk.org/blog/2022/03/25/perspectives-on-async-cancellation/ would be helpful to explain.

in tokio, when we await the JoinHandle, it returns JoinResult, not directly T. JoinResult is the enum of Ok and JoinError. JoinError saves the information whether task is cancelled or panicked. (https://docs.rs/tokio/latest/tokio/task/struct.JoinError.html)

panic is not my interest, I only have a interest abort() function of JoinHandle and Cancellation.

temeddix commented 1 month ago

Oh I see. Indeed adding abort to JoinHandle would make tokio_with_wasm more feature-complete. I will try to implement that feature myself, though there's no guarantee about when it would be done because I'm a bit busy these days. Meanwhile, this repository is open for PRs :)

temeddix commented 1 month ago

This is now implemented in version 0.5.0 :D