async_global_executor::Task cancel behavior

The documentation for the 'cancel' method of the async_global_executor::Task struct includes the statement "Note that canceling a task actually wakes it and reschedules one last time". I interpreted this to mean that, after cancel is called, the task will return from an 'await' one more time, perhaps just because this made sense to me... it would give the task an opportunity to recognize that it had been cancelled and perform cleanup. It's not, however, clear that this is happening. Example code ():

[dependencies]
async-std = "1.12"
async-executor = "1.5.0"
async-global-executor = "2.3"
futures = "0.3.0"
fn main() {
    let task1 = async_global_executor::spawn(async {
        async_std::task::sleep(std::time::Duration::from_secs(60)).await;
        println!("task_one: exiting");
    });

    let task2 = async_global_executor::spawn(async {
        async_std::task::sleep(std::time::Duration::from_secs(60)).await;
        println!("task_two: exiting");
    });

    futures::executor::block_on(async_std::task::sleep(std::time::Duration::from_secs(5)));
    futures::executor::block_on(task1.cancel());
    futures::executor::block_on(task2.cancel());
}

I expected the print statements to be executed upon cancelation, but they were not. Am I interpreting the documentation completely wrong, or is there something else more subtle going on?

It wont be polled one more time. It's just that they want the actual cancellation to happen on the runtime thread, rather than on the thread you called cancel from, so they schedule it for execution, and cancel it instead of polling it when it gets through the queue.

Got it. Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.