diff --git a/tokio/src/task/spawn.rs b/tokio/src/task/spawn.rs index 3c577b82d86..065d38f54b5 100644 --- a/tokio/src/task/spawn.rs +++ b/tokio/src/task/spawn.rs @@ -127,7 +127,13 @@ cfg_rt! { T: Future + Send + 'static, T::Output: Send + 'static, { - spawn_inner(future, None) + // preventing stack overflows on debug mode, by quickly sending the + // task to the heap. + if cfg!(debug_assertions) && std::mem::size_of::() > 2048 { + spawn_inner(Box::pin(future), None) + } else { + spawn_inner(future, None) + } } #[cfg_attr(tokio_track_caller, track_caller)]