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

Suggest RUST_MIN_STACK workaround on overflow #122847

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
6 changes: 5 additions & 1 deletion compiler/rustc_driver_impl/src/signal_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Signal handler for rustc
//! Primarily used to extract a backtrace from stack overflow

use rustc_interface::util::{DEFAULT_STACK_SIZE, STACK_SIZE};
use std::alloc::{alloc, Layout};
use std::{fmt, mem, ptr};

Expand Down Expand Up @@ -100,7 +101,10 @@ extern "C" fn print_stack_trace(_: libc::c_int) {
written += 1;
}
raw_errln!("note: we would appreciate a report at https://github.com/rust-lang/rust");
written += 1;
// get the current stack size WITHOUT blocking and double it
let new_size = STACK_SIZE.get().copied().unwrap_or(DEFAULT_STACK_SIZE) * 2;
raw_errln!("help: you can increase rustc's stack size by setting RUST_MIN_STACK={new_size}");
written += 2;
if written > 24 {
// We probably just scrolled the earlier "we got SIGSEGV" message off the terminal
raw_errln!("note: backtrace dumped due to SIGSEGV! resuming signal");
Expand Down
33 changes: 18 additions & 15 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ pub fn add_configuration(cfg: &mut Cfg, sess: &mut Session, codegen_backend: &dy
}
}

const STACK_SIZE: usize = 8 * 1024 * 1024;

fn get_stack_size() -> Option<usize> {
// FIXME: Hacks on hacks. If the env is trying to override the stack size
// then *don't* set it explicitly.
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;

fn init_stack_size() -> usize {
// Obey the environment setting or default
*STACK_SIZE.get_or_init(|| {
env::var_os("RUST_MIN_STACK")
.map(|os_str| os_str.to_string_lossy().into_owned())
Comment on lines +57 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the into_owned() cloning:

Suggested change
env::var_os("RUST_MIN_STACK")
.map(|os_str| os_str.to_string_lossy().into_owned())
env::var_os("RUST_MIN_STACK")
.as_ref()
.map(|os_str| os_str.to_string_lossy())

// ignore if it is set to nothing
.filter(|s| s.trim() != "")
.map(|s| s.trim().parse::<usize>().unwrap())
Comment on lines +60 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to .unwrap() here? The UX on a bad RUST_MIN_STACK wouldn't be great. Given the usage of .filter() and .unwrap_or() outside of this, does it mean we want to silently ignore bad values (e.g., RUST_MIN_STACK=" ")?

Suggested change
.filter(|s| s.trim() != "")
.map(|s| s.trim().parse::<usize>().unwrap())
.and_then(|s| s.trim().parse::<usize>().ok())

or, if we don't want to silently ignore the erroneous value:

Suggested change
.filter(|s| s.trim() != "")
.map(|s| s.trim().parse::<usize>().unwrap())
.map(|s| s.trim().parse::<usize>().expect("`RUST_MIN_STACK` env var to be a `usize`))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread 'main' panicked at compiler/rustc_interface/src/util.rs:61:48:
called Result::unwrap() on an Err value: ParseIntError { kind: InvalidDigit }

Huh, I thought for some reason it'd look better than that.

// otherwise pick a consistent default
.unwrap_or(DEFAULT_STACK_SIZE)
})
}

pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
Expand All @@ -66,10 +74,7 @@ pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
// the parallel compiler, in particular to ensure there is no accidental
// sharing of data between the main thread and the compilation thread
// (which might cause problems for the parallel compiler).
let mut builder = thread::Builder::new().name("rustc".to_string());
if let Some(size) = get_stack_size() {
builder = builder.stack_size(size);
}
let builder = thread::Builder::new().name("rustc".to_string()).stack_size(init_stack_size());

// We build the session globals and run `f` on the spawned thread, because
// `SessionGlobals` does not impl `Send` in the non-parallel compiler.
Expand Down Expand Up @@ -120,7 +125,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
});
}

let mut builder = rayon::ThreadPoolBuilder::new()
let builder = rayon::ThreadPoolBuilder::new()
.thread_name(|_| "rustc".to_string())
.acquire_thread_handler(jobserver::acquire_thread)
.release_thread_handler(jobserver::release_thread)
Expand All @@ -144,10 +149,8 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
on_panic.disable();
})
.unwrap();
});
if let Some(size) = get_stack_size() {
builder = builder.stack_size(size);
}
})
.stack_size(init_stack_size());

// We create the session globals on the main thread, then create the thread
// pool. Upon creation, each worker thread created gets a copy of the
Expand Down
Loading