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

**URL:** <https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096>\
**Category:** help\
**Created:** [September 5, 2024, 9:46am UTC](https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096 "2024-09-05T09:46:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![linrongbin16](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/linrongbin16/32/42976_2.png) [@linrongbin16](https://users.rust-lang.org/u/linrongbin16)\
**Post date:** [September 5, 2024, 9:46am UTC](https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096/1 "2024-09-05T09:46:27Z")

</div>

hi, I'm writing a TUI executable, I use [tokio::select](https://docs.rs/tokio/latest/tokio/macro.select.html) on [crossterm::event::EventStream](https://docs.rs/crossterm/latest/crossterm/event/struct.EventStream.html) to achieve a non-blocking loop to receive user keyboard/mouse events.

At the same time, I also want to create an async task queue that can run some generic tasks: read/write files, update data logic, delay/timeout methods.

The word _ **task** _ here is similar to function pointers with a global context in C/C++.

After I read some documents, I found rust and tokio provide the [tokio::task::JoinSet](https://docs.rs/tokio/latest/tokio/task/join_set/struct.JoinSet.html#), it could help me create new async tasks, and run them in multiple green threads. And also possible to abort all the tasks, or wait for them done.

That's probably what I want. But I still have 1 more question:

The `JoinSet` is not a future Stream, so I cannot use `tokio::select` on it. So how could I know what task is completed?

If I don't select it, but keep create new tasks. Will it increase memory usage along with the tasks created, and finally get out of memory and crash the TUI application?

---

<div class="post-metadata">

**Author:** ![farnz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/farnz/32/14366_2.png) [@farnz](https://users.rust-lang.org/u/farnz)\
**Post date:** [September 5, 2024, 9:58am UTC](https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096/2 "2024-09-05T09:58:23Z")

</div>

`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`](https://docs.rs/tokio-util/latest/tokio_util/task/task_tracker/struct.TaskTracker.html#comparison-to-joinset) 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`](https://docs.rs/futures/latest/futures/stream/fn.poll_fn.html) to turn [`JoinSet::poll_join_next`](https://docs.rs/tokio/latest/tokio/task/struct.JoinSet.html#method.poll_join_next) into a stream

---

<div class="post-metadata">

**Author:** ![linrongbin16](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/linrongbin16/32/42976_2.png) [@linrongbin16](https://users.rust-lang.org/u/linrongbin16)\
**Post date:** [September 5, 2024, 10:05am UTC](https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096/3 "2024-09-05T10:05:13Z")

</div>

Thanks @farnz , the 2nd option seems really fit my requirement! I will have a try!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [December 4, 2024, 10:05am UTC](https://users.rust-lang.org/t/do-i-need-to-clean-the-completed-tasks-in-tokio-joinset/117096/4 "2024-12-04T10:05:14Z")

</div>

Closed on December 4, 2024, 10:05am UTC

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.
