Skip to content
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

rt: fix default thread number logic #2457

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tokio/src/runtime/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions tokio/tests/rt_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}