Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maitake: task cancellation #264

Closed
hawkw opened this issue Jul 26, 2022 · 2 comments · Fixed by #286
Closed

maitake: task cancellation #264

hawkw opened this issue Jul 26, 2022 · 2 comments · Fixed by #286
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request

Comments

@hawkw
Copy link
Owner

hawkw commented Jul 26, 2022

PR #261 adds a JoinHandle type for maitake's tasks. This allows awaiting the completion of a task. In most other async runtimes, JoinHandle types also provide a way to remotely cancel/abort a task. We should implement that.

This would work by setting a status bit indicating the task is cancelled. The code for polling a task would then check this bit when transitioning to the polling state. If the bit is set, the task transitions to terminated, and drops the inner future.

@hawkw hawkw added kind/enhancement New feature or request good first issue Good for newcomers labels Jul 26, 2022
@hawkw hawkw added the crate/maitake Related to the `maitake` crate label Jul 26, 2022
@jamesmunns
Copy link
Collaborator

It might be interesting to be able to handle this at the leaf future level as well, to simulate task cancellations on violation of something.

For example, my idea would basically be to allow the allocator to "kill" a task if it asks for something like 240TiB of memory.

@hawkw
Copy link
Owner Author

hawkw commented Jul 30, 2022

@jamesmunns yeah, i think a TaskRef::cancel API + implementing #277 (a way to get the currently-polling task as a TaskRef) would be sufficient to allow leaf futures to cancel their parents.

@hawkw hawkw removed the good first issue Good for newcomers label Sep 2, 2022
@hawkw hawkw linked a pull request Sep 2, 2022 that will close this issue
hawkw added a commit that referenced this issue Sep 16, 2022
This branch implements cancellation for spawned tasks. A task can be
cancelled through its `JoinHandle` (`JoinHandle::cancel`), or through a
`TaskRef` to it (`TaskRef::cancel`). When a task is cancelled, a bit is
set on its state field indicating that it has been canceled, and the
task is woken.

A leaf future inside a task can cancel that task by calling
`Scheduler::current_task()` to get a `Taskref` to itself, and then
canceling that `TaskRef`.

When the scheduler polls a task with the canceled bit set, it doesn't
poll the future, but instead notifies the task's `JoinHandle` and drops
its own reference to that task. Once the `JoinHandle` and all `TaskRef`s
that own reference counts for that task are dropped, the task can be
deallocated.

A task may be canceled in a state where the `Future` has completed but
the `JoinHandle` hasn't consumed the `Output` from the `Future` yet. In
that case, the `JoinHandle` still returns an error, but the error
indicates that the task was canceled after completion, and allows
recovering the future's `Output`. anyway.

Closes #264
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants