From 690146e3115f615818ec6927eb56fab157221504 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Tue, 15 Apr 2025 05:52:23 +0800 Subject: [PATCH] fix(macros): invoke handler stack overflow (#13217) * Fix invoke handler stack overflow * Format and inline iife in release build * Add change file * The comment should be one level up --- .../invoke-handler-stack-overflow-on-debug.md | 5 ++++ crates/tauri-macros/src/command/wrapper.rs | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 .changes/invoke-handler-stack-overflow-on-debug.md diff --git a/.changes/invoke-handler-stack-overflow-on-debug.md b/.changes/invoke-handler-stack-overflow-on-debug.md new file mode 100644 index 000000000..5f89ae2f1 --- /dev/null +++ b/.changes/invoke-handler-stack-overflow-on-debug.md @@ -0,0 +1,5 @@ +--- +tauri: patch:bug +--- + +Fix large number of commands with large structs as parameters causing stack overflow on debug build on Windows diff --git a/crates/tauri-macros/src/command/wrapper.rs b/crates/tauri-macros/src/command/wrapper.rs index 768cb8c62..683e9f6f5 100644 --- a/crates/tauri-macros/src/command/wrapper.rs +++ b/crates/tauri-macros/src/command/wrapper.rs @@ -280,18 +280,25 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream { #maybe_macro_export #[doc(hidden)] macro_rules! #wrapper { - // double braces because the item is expected to be a block expression - ($path:path, $invoke:ident) => {{ - #[allow(unused_imports)] - use #root::ipc::private::*; - // prevent warnings when the body is a `compile_error!` or if the command has no arguments - #[allow(unused_variables)] - let #root::ipc::Invoke { message: #message, resolver: #resolver, acl: #acl } = $invoke; + // double braces because the item is expected to be a block expression + ($path:path, $invoke:ident) => { + // The IIFE here is for preventing stack overflow on Windows debug build, + // see https://github.com/tauri-apps/tauri/issues/12488 + { + #[cfg_attr(not(debug_assertions), inline(always))] + move || { + #[allow(unused_imports)] + use #root::ipc::private::*; + // prevent warnings when the body is a `compile_error!` or if the command has no arguments + #[allow(unused_variables)] + let #root::ipc::Invoke { message: #message, resolver: #resolver, acl: #acl } = $invoke; - #maybe_span + #maybe_span - #body - }}; + #body + } + }() + }; } // allow the macro to be resolved with the same path as the command function