Skip to content

Commit

Permalink
Add an env var to artificially limit the stack size
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Jan 18, 2024
1 parent 41ae1d5 commit 5a63635
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions crates/puffin-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::path::PathBuf;
use std::process::ExitCode;
use std::str::FromStr;
Expand Down Expand Up @@ -710,9 +711,31 @@ async fn inner() -> Result<ExitStatus> {
}
}

#[tokio::main]
async fn main() -> ExitCode {
match inner().await {
fn tokio_main() -> Result<ExitStatus> {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(inner())
}

fn main() -> ExitCode {
let result = if let Ok(stack_max) = env::var("PUFFIN_STACK_SIZE") {
// Artificially limit the stack size to test for stack overflows. Windows has a default stack size of 1MB,
// which is lower than the linux and mac default.
// https://learn.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=msvc-170
std::thread::Builder::new()
.stack_size(stack_max.parse().expect("Invalid stack size"))
.spawn(tokio_main)
.expect("Tokio executor failed, was there a panic?")
.join()
.expect("Tokio executor failed, was there a panic?")
} else {
tokio_main()
};

// Wait for thread to join
match result {
Ok(code) => code.into(),
Err(err) => {
#[allow(clippy::print_stderr)]
Expand Down

0 comments on commit 5a63635

Please sign in to comment.