Skip to content

Lock file retained when app exits abnormally #32

@YinMo19

Description

@YinMo19

As title.

fix method: remove "$HOME/Library/Application Support/org.xdsec.wsrx/.rx.is.alive"

Code fix: ( I don't know whether it is a good design so no pr)

--- a/crates/desktop/src/launcher.rs
+++ b/crates/desktop/src/launcher.rs
@@ -1,4 +1,3 @@
-use async_compat::Compat;
 use directories::ProjectDirs;
 use slint::PlatformError;
 use tracing::info;
@@ -18,33 +17,54 @@ pub fn setup() -> Result<MainWindow, PlatformError> {
     let lock_file = proj_dirs.data_local_dir().join(".rx.is.alive");
 
     if lock_file.exists() {
-        eprintln!("Another instance of the application is already running.");
-        let api_port = std::fs::read_to_string(&lock_file).unwrap_or_else(|_| {
-            eprintln!("Failed to read lock file");
-            std::fs::remove_file(&lock_file).unwrap_or_else(|_| {
-                eprintln!("Failed to remove lock file");
-            });
-            std::process::exit(1);
-        });
-        eprintln!("Notify the other instance to raise...");
-        slint::spawn_local(Compat::new(async move {})).expect("Failed to spawn thread");
-        let client = reqwest::blocking::Client::new();
-        match client
-            .post(format!("http://127.0.0.1:{api_port}/popup"))
-            .header("User-Agent", format!("wsrx/{}", env!("CARGO_PKG_VERSION")))
-            .send()
-        {
-            Ok(_) => {
+        eprintln!("Detected existing instance lock file. Trying to notify running app...");
+
+        let api_port = match std::fs::read_to_string(&lock_file) {
+            Ok(port) => port.trim().to_owned(),
+            Err(err) => {
+                eprintln!("Failed to read lock file: {err}. Removing stale lock file.");
+                std::fs::remove_file(&lock_file).unwrap_or_else(|_| {
+                    eprintln!("Failed to remove lock file");
+                });
+                String::new()
+            }
+        };
+
+        let notify_result = if api_port.parse::<u16>().is_ok() {
+            reqwest::blocking::Client::builder()
+                .no_proxy()
+                .build()
+                .map_err(|e| format!("failed to create HTTP client: {e}"))
+                .and_then(|client| {
+                    client
+                        .post(format!("http://127.0.0.1:{api_port}/popup"))
+                        .header("User-Agent", format!("wsrx/{}", env!("CARGO_PKG_VERSION")))
+                        .send()
+                        .map_err(|e| format!("request failed: {e}"))
+                        .and_then(|response| {
+                            if response.status().is_success() {
+                                Ok(())
+                            } else {
+                                Err(format!("unexpected response status {}", response.status()))
+                            }
+                        })
+                })
+        } else {
+            Err("invalid api port in lock file".to_string())
+        };
+
+        match notify_result {
+            Ok(()) => {
                 eprintln!("Notification sent.");
+                std::process::exit(0);
             }
-            Err(e) => {
-                eprintln!("Failed to send notification: {e}, removing lock file.");
+            Err(err) => {
+                eprintln!("Failed to notify existing app ({err}). Removing stale lock file.");
                 std::fs::remove_file(&lock_file).unwrap_or_else(|_| {
                     eprintln!("Failed to remove lock file");
                 });
             }
         }
-        std::process::exit(0);
     }
 
     let ui = MainWindow::new()?;

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions