1 Commits

Author SHA1 Message Date
Steve Lau
1759e0e56e refactor: add a timeout to open()
Adds a 500ms timeout to open(), to prevent the open action that hangs
from hanging indefinitely.
2025-12-22 12:42:45 +08:00
2 changed files with 145 additions and 124 deletions

View File

@@ -17,6 +17,8 @@ Information about release notes of Coco App is provided here.
### ✈️ Improvements
- refactor: add a timeout to open() #1025
## 0.10.0 (2025-12-19)
### ❌ Breaking changes

View File

@@ -148,9 +148,15 @@ pub(crate) async fn open(
extra_args: Option<HashMap<String, Json>>,
) -> Result<(), String> {
use crate::util::open as homemade_tauri_shell_open;
use std::process::Command;
use tokio::process::Command;
use tokio::time::Duration;
use tokio::time::timeout;
match on_opened {
let on_opened_clone = on_opened.clone();
// Put the main logic in an async closure so that we can `time::timeout()`
// it
let async_closure = async move {
match on_opened_clone {
OnOpened::Application { app_path } => {
log::debug!("open application [{}]", app_path);
@@ -189,7 +195,7 @@ pub(crate) async fn open(
if let Some(args) = action.args {
cmd.args(args);
}
let output = cmd.output().map_err(|e| e.to_string())?;
let output = cmd.output().await.map_err(|e| e.to_string())?;
// Sometimes, we wanna see the result in logs even though it doesn't fail.
log::debug!(
"executing open(Command) result, exit code: [{}], stdout: [{}], stderr: [{}]",
@@ -230,7 +236,7 @@ pub(crate) async fn open(
}
cmd.arg(&url);
let output = cmd.output().map_err(|e| format!("failed to spawn [open] due to error [{}]", e))?;
let output = cmd.output().await.map_err(|e| format!("failed to spawn [open] due to error [{}]", e))?;
if !output.status.success() {
return Err(format!(
@@ -294,6 +300,19 @@ pub(crate) async fn open(
}
Ok(())
};
match timeout(Duration::from_millis(500), async_closure).await {
Ok(res) => res,
Err(_timed_out) => {
log::warn!("executing open(on_opened: [{:?}]) timed out", on_opened);
Err(format!(
"executing open(on_opened: {:?}) timed out",
on_opened
))
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]