Skip to content

Commit

Permalink
Box::pin(run()) and 2MB test stack (#4851)
Browse files Browse the repository at this point in the history
By using `Box::pin(run())` we can reduce the artificial stack size for
running tests on windows in debug mode from 8MB to 2MB. I've checked and
1MB/no custom stack size still fail tests, e.g.
`add_workspace_editable`.
  • Loading branch information
konstin committed Jul 8, 2024
1 parent b21f322 commit a76d04b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,19 +978,21 @@ async fn run_project(
}

fn main() -> ExitCode {
// 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
// We support increasing the stack size to avoid stack overflows in debug mode on Windows. In
// addition, we box types and futures in various places. This includes the `Box::pin(run())`
// here, which prevents the large (non-send) main future alone from overflowing the stack.
let result = if let Ok(stack_size) = env::var("UV_STACK_SIZE") {
// Artificially limit or increase the stack size to test without stack overflows in debug
// mode. 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
let stack_size = stack_size.parse().expect("Invalid stack size");
let tokio_main = move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_stack_size(stack_size)
.build()
.expect("Failed building the Runtime");
let result = runtime.block_on(run());
// Box the large main future to avoid stack overflows.
let result = runtime.block_on(Box::pin(run()));
// Avoid waiting for pending tasks to complete.
//
// The resolver may have kicked off HTTP requests during resolution that
Expand All @@ -1010,7 +1012,8 @@ fn main() -> ExitCode {
.enable_all()
.build()
.expect("Failed building the Runtime");
let result = runtime.block_on(run());
// Box the large main future to avoid stack overflows.
let result = runtime.block_on(Box::pin(run()));
runtime.shutdown_background();
result
};
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl TestContext {
if cfg!(all(windows, debug_assertions)) {
// TODO(konstin): Reduce stack usage in debug mode enough that the tests pass with the
// default windows stack of 1MB
command.env("UV_STACK_SIZE", (8 * 1024 * 1024).to_string());
command.env("UV_STACK_SIZE", (2 * 1024 * 1024).to_string());
}
}

Expand Down

0 comments on commit a76d04b

Please sign in to comment.