-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokio::task::spawn_blocking panics when exceeding the thread limit #2309
Comments
If the OS limit is readable we could set the max threadpool size to Returning an error is rather ugly. I would rather keep the current way, and ask users to increase limits. I think users might already get other panics if opening objects above limits (e.g files). |
What is the status on this? |
I've hit this issue recently in a production environment (application running in a docker container, based on very vanilla Executing the task on an already-existing blocking thread is a potential solution sometimes, but I can definitely see how it might lead to deadlocks in pathological cases. In my particular application, I am actually able to handle a failure gracefully and so a guaranteed non-panicing variant of blocking task spawn API would be welcome. A file handle limit example mentioned in this thread isn't quite the same as this panic, since nothing unwraps the file open result. What makes this issue particularly annoying is that it can happen quite sporadically and it's hard to catch it in the act to really diagnose things. One potential workaround that I have considered was to just force spawn all the blocking worker threads on startup and turn off the timeout. This may be a reasonable thing to do for 12 thread case, but not so much for default 512 max :) |
I've received users' feedback hitting this issue. The thread limit is high and unlikely to be reached. However the panic still occurs |
After some digging, this boils down to From
So there's something wrong with the system resource and it's not tokio's fault. Maybe one way forward is to create a spawn_blocking version that returns a Result and doesn't panic. |
Well, spawn_blocking uses a thread pool. If it is unable to spawn more threads, it could just keep using the ones it has. Of course if it's unable to spawn even on spawn_blocking thread, then there's a problem. |
I started playing around this idea. |
Avoid panicking when the OS reaches the limit of the number of threads / processes and the error is temporary. Spawning a new thread is not mandatory to make progress as long as there is a least one thread in the pool already processing the task queue. Fixes: tokio-rs#2309
If a Tokio application is running within an environment with a limited number of processes/threads (e.g. somewhere like the playground that uses cgroup limiting to a max of 512 processes) it will panic if this limit is below maximum number of blocking tasks
Repro: run this example on the playground or within a shell with
ulimit -u 512
appliedlogs
backtrace
Originally posted by @Nemo157 in #2143 (comment)
The text was updated successfully, but these errors were encountered: