Tokio Runtime - what happens when a thread panics?

There are two specific cases I would like to ask about here.

  1. Worker threads - e.g. the limit created from Builder::worker_threads and who get passed work via Runtime::spawn.
  2. Blocking threads - the limit created from Builder::max_blocking_threads and who get spawned/created from Runtime::spawn_blocking.

In both of these cases, if the provided Future/Function panic, what happens to the thread where the work was being executed? I know you can find if the task panicked via awaiting on the JoinHandle and then checking is_panic on the error.

My main concern is if I have 4 worker threads, and one of them is executing a future which panics, does that reduce the number of worker threads I have to 3? Or will tokio internally replace the thread and/or catch the panic so it doesn't cause the thread to stop working. (Same for blocking threads - but I guess since those get created on the fly, it's probably less of a concern).

Tokio will just catch the panic. It has the exact same effect as when the task exits normally, if you treat the panic as the return value.

This is the same for blocking tasks as well.

2 Likes

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.