Browse Source

fix(core): deadlock when creating new window with tracing feature (#11213)

Lucas Fernandes Nogueira 10 months ago
parent
commit
c72cd45ccd

+ 5 - 0
.changes/fix-deadlock-tracing-window-creation.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch:bug
+---
+
+Fixes a deadlock on window creation when the `tracing` feature is enabled.

+ 28 - 12
core/tauri/src/window.rs

@@ -351,19 +351,35 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
     }
     .map(|window| self.manager.attach_window(self.app_handle.clone(), window))?;
 
-    self.manager.eval_script_all(format!(
-      "window.__TAURI_METADATA__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
-      window_labels_array = serde_json::to_string(&self.manager.labels())?,
-    ))?;
+    let manager = self.manager.clone();
+    let label = window.label().to_string();
+
+    let window_created_hook = move || {
+      manager.eval_script_all(format!(
+        "window.__TAURI_METADATA__.__windows = {window_labels_array}.map(function (label) {{ return {{ label: label }} }})",
+        window_labels_array = serde_json::to_string(&manager.labels())?,
+      ))?;
+
+      manager.emit_filter(
+        "tauri://window-created",
+        None,
+        Some(WindowCreatedEvent {
+          label: label.clone(),
+        }),
+        |w| w.label() != label,
+      )?;
+
+      crate::Result::Ok(())
+    };
 
-    self.manager.emit_filter(
-      "tauri://window-created",
-      None,
-      Some(WindowCreatedEvent {
-        label: window.label().into(),
-      }),
-      |w| w != &window,
-    )?;
+    #[cfg(not(feature = "tracing"))]
+    window_created_hook()?;
+    #[cfg(feature = "tracing")]
+    std::thread::spawn(move || {
+      if let Err(e) = window_created_hook() {
+        log::error!("failed to trigger window creation hooks: {e}");
+      }
+    });
 
     Ok(window)
   }

+ 2 - 2
examples/api/src-tauri/Cargo.lock

@@ -4027,7 +4027,7 @@ checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae"
 
 [[package]]
 name = "tauri"
-version = "1.7.2"
+version = "1.8.0"
 dependencies = [
  "anyhow",
  "base64 0.22.1",
@@ -4095,7 +4095,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-build"
-version = "1.5.4"
+version = "1.5.5"
 dependencies = [
  "anyhow",
  "cargo_toml",