From 62e779f82d7c9427c33484baa676c09a1917b0d6 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 17 May 2023 15:49:57 -0600 Subject: fix(runtime): Box the main future to avoid blowing up the stack (#19155) This fixes `Unhandled exception at [...] Stack overflow` on Windows, caused by the large size of the main future. --- runtime/tokio_util.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'runtime') diff --git a/runtime/tokio_util.rs b/runtime/tokio_util.rs index ce6ef305f..d723006bd 100644 --- a/runtime/tokio_util.rs +++ b/runtime/tokio_util.rs @@ -16,14 +16,26 @@ pub fn create_basic_runtime() -> tokio::runtime::Runtime { .unwrap() } +#[inline(always)] pub fn create_and_run_current_thread(future: F) -> R where F: std::future::Future + 'static, R: Send + 'static, { let rt = create_basic_runtime(); + + // Since this is the main future, we want to box it in debug mode because it tends to be fairly + // large and the compiler won't optimize repeated copies. We also make this runtime factory + // function #[inline(always)] to avoid holding the unboxed, unused future on the stack. + + #[cfg(debug_assertions)] + // SAFETY: this this is guaranteed to be running on a current-thread executor + let future = Box::pin(unsafe { MaskFutureAsSend::new(future) }); + + #[cfg(not(debug_assertions))] // SAFETY: this this is guaranteed to be running on a current-thread executor let future = unsafe { MaskFutureAsSend::new(future) }; + let join_handle = rt.spawn(future); rt.block_on(join_handle).unwrap().into_inner() } -- cgit v1.2.3