Skip to content

Commit

Permalink
fix: generate Context inside a thread (#10734)
Browse files Browse the repository at this point in the history
* generate `Context` inside a thread

fix #9882

this is a workaround for #9882 due to windows having a small
stack size for the main thread (1MiB) versus other platforms which
have 8MiB. the true fix would be to lower the generated code
stack size, but with lots a plugins, there are lots of ACL
configurations.

* add change file [skip ci]

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
chippers and lucasfernog authored Aug 23, 2024
1 parent 91e9e78 commit 88bc357
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/fix-context-stack-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-codegen": patch:bug
---

Generate context in a separate thread to prevent a stack overflow.
18 changes: 17 additions & 1 deletion core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
quote!()
};

Ok(quote!({
let context = quote!({
#[allow(unused_mut, clippy::let_and_return)]
let mut context = #root::Context::new(
#config,
Expand All @@ -507,6 +507,22 @@ pub fn context_codegen(data: ContextData) -> EmbeddedAssetsResult<TokenStream> {
#maybe_config_parent_setter

context
});

Ok(quote!({
let thread = ::std::thread::Builder::new()
.name(String::from("generated tauri context creation"))
.stack_size(8 * 1024 * 1024)
.spawn(|| #context)
.expect("unable to create thread with 8MiB stack");

match thread.join() {
Ok(context) => context,
Err(_) => {
eprintln!("the generated Tauri `Context` panicked during creation");
::std::process::exit(101);
}
}
}))
}

Expand Down

0 comments on commit 88bc357

Please sign in to comment.