Do I need to clean the completed tasks in tokio::task::JoinSet?

JoinSet keeps the result of running a task to completion available until you use join_all, join_next, or try_join_next to get the result of running a task. Memory usage will go up as long as this isn't done.

You have three directions you can go in:

  1. Use tokio_util::TaskTracker to track tasks. This is a lot like JoinSet, but it immediately drops the result of running a task, rather than keeping it around for later.
  2. Use tokio::select! on JoinSet::join_next as well as EventStream::next, to pull out completed tasks.
  3. Make use of futures::stream::poll_fn to turn JoinSet::poll_join_next into a stream
2 Likes