Skip to content

Feature Request: Add AbortHandle::is_aborted() #2709

@ZhennanWu

Description

@ZhennanWu

Currently there is already a public Abortable::is_aborted() method, and there seems no obstacle in adding a AbortHandle::is_aborted()

Motivation

Sometimes Abortable needs to be accompanied by cooperative cancellation mechanism such as a atomic boolean flag. Rather than using another repetitive atomic variable, the AbortInner already hosts such atomic variable and the AbortHandle seems to be a good way to expose it.

Here is a hypothetical scenario where Abortable needs to be accompanied by cooperative cancellation mechanism. Suppose we have a blocking Mutex protecting a very tiny shared state, and we wish to stop another task from modifying it by calling its AbortHandle

// Aborter
// ...
let (new_abort_handle, new_abort_reg) = AbortHandle::new_pair();
let cooperative_flag = Asc::new(AtomicBool::new(false)); // new_abort_handle.clone();
{
    let mut guard = mutex.lock();
    // Take out the old task's handle
    let old_abort_handle = std::mem::replace(guard.abort_handle, new_abort_handle);
    old_abort_handle.abort();
    // A modification to the cooperative flag is needed here
    drop(guard);
}
// Spawn a new task using the new handle
tokio::spawn(Abortable::new(some_async_function(args, cooperative_flag), new_abort_reg);
// We wish no modifications to the shared state from the old task from here on

// Abortee
let mut guard = mutex.lock();
// A cooperative checking is needed here
write_into_shared_state(guard.state);
drop(guard);

Since we used a blocking Mutex to protect our state (which is warranted in an async scenario) , it is very likely that our target Future is blocked on the mutex call when the AbortHandle::abort() was called. To solve this issue we would need a cooperative checking in our code. Exposing AbortHandle::is_aborted() would save the user from another atomic boolean flag.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions