diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 56fb898adca..7c7b2d3f0f0 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 { + 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(); diff --git a/tokio/tests/rt_threaded.rs b/tokio/tests/rt_threaded.rs index 9c95afd5ae2..ad063348f68 100644 --- a/tokio/tests/rt_threaded.rs +++ b/tokio/tests/rt_threaded.rs @@ -322,6 +322,16 @@ fn multi_threadpool() { done_rx.recv().unwrap(); } +// Testing this does not panic +#[test] +fn max_threads() { + let _rt = tokio::runtime::Builder::new() + .threaded_scheduler() + .max_threads(1) + .build() + .unwrap(); +} + fn rt() -> Runtime { Runtime::new().unwrap() }