diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-05-17 15:49:57 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-17 15:49:57 -0600 |
commit | 62e779f82d7c9427c33484baa676c09a1917b0d6 (patch) | |
tree | 18ef92445e759d5b4a3e4387e3b9f0cf7483baf5 /runtime/tokio_util.rs | |
parent | 41f618a1df6bb8c66d7968ac64456139b9f4c197 (diff) |
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.
Diffstat (limited to 'runtime/tokio_util.rs')
-rw-r--r-- | runtime/tokio_util.rs | 12 |
1 files changed, 12 insertions, 0 deletions
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<F, R>(future: F) -> R where F: std::future::Future<Output = R> + '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() } |