async-rs/async-std

Directly kill task without waiting for finish

DasLixou opened this issue · 2 comments

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

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.

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).