Browse Source

fix(core): `safe_block_on` usage on async contexts, closes #3505 (#3513)

Lucas Fernandes Nogueira 3 năm trước cách đây
mục cha
commit
0163489ed6
2 tập tin đã thay đổi với 9 bổ sung3 xóa
  1. 5 0
      .changes/fix-safe-block-on.md
  2. 4 3
      core/tauri/src/async_runtime.rs

+ 5 - 0
.changes/fix-safe-block-on.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Fixes `Command::output` and `Command::status` deadlock when running on async commands.

+ 4 - 3
core/tauri/src/async_runtime.rs

@@ -285,10 +285,11 @@ where
   F: Future + Send + 'static,
   F::Output: Send + 'static,
 {
-  if tokio::runtime::Handle::try_current().is_ok() {
+  if let Ok(handle) = tokio::runtime::Handle::try_current() {
     let (tx, rx) = std::sync::mpsc::sync_channel(1);
-    spawn(async move {
-      tx.send(task.await).unwrap();
+    let handle_ = handle.clone();
+    handle.spawn_blocking(move || {
+      tx.send(handle_.block_on(task)).unwrap();
     });
     rx.recv().unwrap()
   } else {