async-rs / async-std

Async version of the Rust standard library
https://async.rs
Apache License 2.0
3.94k stars 339 forks source link

Directly kill task without waiting for finish #1038

Open DasLixou opened 2 years ago

DasLixou commented 2 years ago

hi im new to rust and stumbled across this library. currently i try to do a simple music application and have a music player running inside a task. but the problem is that i now cant directly stop the task. i tried it with JoinHandler.cancel().await but that just waits till the task is finished. is there a way to directly kill it? thanks :D

notgull commented 2 years ago

cancel() waits for the task to reappear in the executor's queue for the last time before it dies. For async tasks, this should be instant. However, if it isn't instant, what I'd suspect is that your task contains a blocking call.

ZhennanWu commented 1 year ago

I agree with the poster that async-std is missing this capability. tokio has a synchronous JoinHandle::abort and smol has the equivalent synchronous cancel-on-drop. async-std currently has absolutely no way of synchronously cancel a task. This is unfortunate since the underlying smol already has a readily available Task::set_canceled(&mut self).

The only thing left is just to introduce a new method JoinHandle::whatever_name_for_sync_cancel(self).