-
Notifications
You must be signed in to change notification settings - Fork 51
Description
Today Task::cancel
is an async fn which does 2 things: marks the task as cancelled and awaits a future for the task to be resolved.
However, since futures are "lazy", this means that in order to mark the task as cancelled, you MUST poll the returned future. This does not provide a way to mark a task as cancelled synchronously, and then just dropping the task. For a concrete example, if you are loading a file, but then that file is updated, it can make sense to cancel the existing task, and not wait for it to actually finish, but to immediately start loading the file again with a replacement task.
So ideally there should be a way to cancel a tasks synchronously. One approach could be to provide a new method cancel_sync
. Another approach could be to make cancel
no longer be an async fn
, but still return impl Future. This could set_cancelled immediately, and then return the cancellation task just the same way. This second approach does have a downside that it is impossible to cancel a task if you can't take ownership though.