From 9922c0e0f5eb338869a33679c5efe43a029d84ac Mon Sep 17 00:00:00 2001 From: John Carmack Date: Tue, 12 Nov 2024 08:31:27 -0800 Subject: [PATCH 1/3] Add run_async_command macro for SQL plugin --- plugins/sql/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/sql/src/lib.rs b/plugins/sql/src/lib.rs index 352ffbec5e..936590980f 100644 --- a/plugins/sql/src/lib.rs +++ b/plugins/sql/src/lib.rs @@ -104,6 +104,12 @@ impl MigrationSource<'static> for MigrationList { } } +macro_rules! run_async_command { + ($cmd:expr) => { + tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on($cmd)) + }; +} + /// Tauri SQL plugin builder. #[derive(Default)] pub struct Builder { @@ -138,7 +144,7 @@ impl Builder { .setup(|app, api| { let config = api.config().clone().unwrap_or_default(); - tauri::async_runtime::block_on(async move { + run_async_command!(async move { let instances = DbInstances::default(); let mut lock = instances.0.write().await; @@ -166,7 +172,7 @@ impl Builder { }) .on_event(|app, event| { if let RunEvent::Exit = event { - tauri::async_runtime::block_on(async move { + run_async_command!(async move { let instances = &*app.state::(); let instances = instances.0.read().await; for value in instances.values() { From 69a41f1df1f039a181b607b97f3eb40d1eb2131f Mon Sep 17 00:00:00 2001 From: John Carmack Date: Tue, 12 Nov 2024 16:21:32 -0800 Subject: [PATCH 2/3] Add explainer comment, convert macro to function, and add changefile. --- .changes/sql-allow-blocking-without-nested-runtime.md | 5 +++++ plugins/sql/src/lib.rs | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 .changes/sql-allow-blocking-without-nested-runtime.md diff --git a/.changes/sql-allow-blocking-without-nested-runtime.md b/.changes/sql-allow-blocking-without-nested-runtime.md new file mode 100644 index 0000000000..ff2c773fea --- /dev/null +++ b/.changes/sql-allow-blocking-without-nested-runtime.md @@ -0,0 +1,5 @@ +--- +"sql": "patch" +--- + +Allow blocking on async code without creating a nested runtime. diff --git a/plugins/sql/src/lib.rs b/plugins/sql/src/lib.rs index 936590980f..adcb3a7265 100644 --- a/plugins/sql/src/lib.rs +++ b/plugins/sql/src/lib.rs @@ -104,10 +104,9 @@ impl MigrationSource<'static> for MigrationList { } } -macro_rules! run_async_command { - ($cmd:expr) => { - tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on($cmd)) - }; +/// Allows blocking on async code without creating a nested runtime. +fn run_async_command(cmd: F) -> F::Output { + tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd)) } /// Tauri SQL plugin builder. @@ -144,7 +143,7 @@ impl Builder { .setup(|app, api| { let config = api.config().clone().unwrap_or_default(); - run_async_command!(async move { + run_async_command(async move { let instances = DbInstances::default(); let mut lock = instances.0.write().await; @@ -172,7 +171,7 @@ impl Builder { }) .on_event(|app, event| { if let RunEvent::Exit = event { - run_async_command!(async move { + run_async_command(async move { let instances = &*app.state::(); let instances = instances.0.read().await; for value in instances.values() { From d4ae5e83e5c2cc6c6490199c64d3d82280e78fdc Mon Sep 17 00:00:00 2001 From: John Carmack Date: Wed, 13 Nov 2024 07:01:36 -0800 Subject: [PATCH 3/3] Add check for nested runtime to run_async_command --- plugins/sql/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/sql/src/lib.rs b/plugins/sql/src/lib.rs index adcb3a7265..c310df296c 100644 --- a/plugins/sql/src/lib.rs +++ b/plugins/sql/src/lib.rs @@ -106,7 +106,11 @@ impl MigrationSource<'static> for MigrationList { /// Allows blocking on async code without creating a nested runtime. fn run_async_command(cmd: F) -> F::Output { - tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd)) + if tokio::runtime::Handle::try_current().is_ok() { + tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd)) + } else { + tauri::async_runtime::block_on(cmd) + } } /// Tauri SQL plugin builder.