diff options
author | Carl Lerche <me@carllerche.com> | 2020-04-28 15:04:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-28 15:04:41 -0700 |
commit | 1bf1928088e5cc545eee447b38da7a785099059f (patch) | |
tree | 592c2c27d393a53dd5808d86583a20daa9a68565 /tokio/src/runtime/builder.rs | |
parent | a26d3aec961fbb748217f853ba33af7007736b8b (diff) |
rt: fix default thread number logic (#2457)
Previously, the function picking the default number of threads for the
threaded runtime did not factor in `max_threads`. Instead, it only used
the value returned by `num_cpus`. However, if `num_cpus` returns a value
greater than `max_threads`, then the function would panic.
This patch fixes the function by limiting the default number of threads
by `max_threads`.
Fixes #2452
Diffstat (limited to 'tokio/src/runtime/builder.rs')
-rw-r--r-- | tokio/src/runtime/builder.rs | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 56fb898a..7c7b2d3f 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -461,10 +461,12 @@ cfg_rt_threaded! { } fn build_threaded_runtime(&mut self) -> io::Result<Runtime> { + use crate::loom::sys::num_cpus; use crate::runtime::{Kind, ThreadPool}; use crate::runtime::park::Parker; + use std::cmp; - let core_threads = self.core_threads.unwrap_or_else(crate::loom::sys::num_cpus); + let core_threads = self.core_threads.unwrap_or_else(|| cmp::min(self.max_threads, num_cpus())); assert!(core_threads <= self.max_threads, "Core threads number cannot be above max limit"); let clock = time::create_clock(); |