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
This commit is contained in:
Carl Lerche 2020-04-28 15:04:41 -07:00 committed by GitHub
parent a26d3aec96
commit 1bf1928088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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();

View File

@ -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()
}