Tokio async function graceful shutdown helpers

As far as I understand, in Rust, every async function gets converted to a class/struct with poll() method.

It means that in many situations it is important to carefully control how those objects die, especially if we want them to be dropped in certain order / gracefully.

I have read documentation of tokio, and it seems it has two things for this:

  • cancellation token to notify "async functions" that they have to stop and
  • they exploit tokio::sync::mpsc::Sender object, when they are dropped, parent knows that all its "children" has stopped.

Hope this makes sense so far.

It appears that pretty much every function (if we are talking about a stack of nested async functions), should have these two parameters:

ct: tokio_util::sync::CancellationToken,
died_signal: tokio::sync::mpsc::Sender<()>
  • Does it make sense to wrap these two things in a dedicated struct?
  • If it makes sense, are there such structs somewhere already?
  • If not, why? It looks like it is a common task...

I've heard some people recommend tokio-graceful for that.

Note also that we're working on a replacement for the mpsc sender "exploit": sync: add `tokio_util::sync::TaskTracker` by Darksonn · Pull Request #6033 · tokio-rs/tokio · GitHub


In case anyone needs something like this, this is what I came up with

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.