Add task/scheduler cancellation API #557
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's a frequent situation where a task runs for a really long time, or just hangs (maybe due to a bug, or intentionally), and we just want to stop the task and move on with life. You might think that using Ctrl+C is the right way to do this, but you'll find that with Julia (and many other languages) that this frequently does not do what you want, and is just as likely to hang or crash your Julia process. This is because the request to "cancel" some running code isn't targeted, and so Julia just interrupts whatever task is running currently, which is frequently not the task that you actually wanted to cancel.
This PR adds a new function,
Dagger.cancel!
, which allows for cancelling DaggerDTask
s in a safe way. Unlike Ctrl+C, this doesn't force the underlying task to stop (that is generally considered unsafe and impossible to always do safely and in a timely manner), but instead just "abandons" the task and lets Dagger's runtime and scheduler move on to working on other queued tasks. This releases any calls towait
orfetch
that were waiting on the cancelledDTask
, and unblocks the processor queues so that other tasks may run.It also provides a way to halt the scheduler and allow it to restart automatically, which can prove useful for automated testing and when certain kinds of hangs occur within the scheduler.
It's expected that this functionality will eventually be wired up to a smarter Ctrl-C, so that users can regain control of a seemingly unresponsive system, or to allow prototyping algorithms in the REPL which may run for a really long time.