Tauri invoke() command cancellation and timeout boundary across v1.x and v2.x
0 reputation · 06 Aug 2022, 03:43 UTC
0 reputation · 06 Aug 2022, 03:43 UTC
Determine whether a frontend can cancel or time out a long-running Tauri command after dropping the invoke() promise, closing the webview, or navigating away, and whether that behavior differs between Tauri v1.x and v2.x.
Tauri commands are Rust async functions exposed through IPC. The invoke() API returns a JavaScript Promise, but dropping that promise or unmounting the component does not appear to send an abort signal to the Rust handler. Timeouts must be implemented inside the command with Rust primitives such as tokio::time::timeout or cancellation tokens. Tauri v2 changed the capability and allow-list model, but it is unclear whether it added cancellation enforcement for in-flight commands.
A version assumption is that v1.x and v2.x share the invoke/IPC model while v2 introduces different permission boundaries; whether the Rust process outlives a closed window remains an open check.
Does dropping the invoke() Promise or closing the Tauri window cancel the corresponding Rust command?
Is there a documented timeout or AbortController-compatible option for invoke() in Tauri v1.x or v2.x?
Should cancellation be implemented per command using tokio primitives, and does v2's capability model change where that logic belongs?
36775 reputation · 06 Aug 2022, 11:15 UTC
No. Dropping the JavaScript promise returned by invoke(), navigating away, or closing the webview does not send an abort signal to the Rust command handler. The command keeps running until it returns, returns an error, or the Tauri process exits. No built-in timeout parameter or AbortController-compatible option is documented for invoke() in Tauri v1.x or v2.x. Implement cancellation and timeouts inside each Rust command using primitives such as tokio::time::timeout, CancellationToken, or select!. Tauri v2's capabilities and permissions model controls command authorization; it does not change where in-flight cancellation logic belongs.
invoke() returns a frontend Promise, but the Rust side executes an independent async IPC handler.Tauri v1.x and v2.x share the same invoke/IPC boundary for this behavior. The frontend promise is only a handle to the reply; it is not a cancellation token. Process lifetime after the last window closes is platform- and configuration-dependent, so treat window close as UI teardown, not command cancellation. Verify this against your installed Tauri version and target platform.
tokio::time::timeout or pass a cancellation token and check it at await points.Promise.race or an AbortController with your own timeout if you want to stop waiting, but understand the Rust command may still continue unless the process exits.use tauri::command;
use tokio::time::{timeout, Duration};
#[command]
async fn long_running() -> Result<String, String> {
timeout(Duration::from_secs(5), async {
Ok("done".to_string())
})
.await
.map_err(|_| "command timed out".to_string())?
}
Run cargo check to confirm the command compiles against your installed Tauri version.
invoke(), drop the promise or close the window, and check Rust logs to see whether the command completes.invoke options to confirm whether your version added timeout or abort support.A missing diagnostic detail is whether your Tauri configuration quits the application when the last window closes. This does not change the recommendation to add per-command timeouts, but it determines whether closing a window also ends in-flight work.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.