From 108a05ce9690e7275fa0f4dedbb562b66ccffbca Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Wed, 19 Nov 2025 22:11:22 +0800 Subject: [PATCH] refactor: replace CommandChildGuard with CommandChild and simplify related methods --- src-tauri/src/core/manager/mod.rs | 8 ++++---- src-tauri/src/core/manager/state.rs | 13 +++++++++---- src-tauri/src/process/guard.rs | 30 ----------------------------- src-tauri/src/process/mod.rs | 2 -- 4 files changed, 13 insertions(+), 40 deletions(-) delete mode 100644 src-tauri/src/process/guard.rs diff --git a/src-tauri/src/core/manager/mod.rs b/src-tauri/src/core/manager/mod.rs index 8f41af64f..8ec5a3c9d 100644 --- a/src-tauri/src/core/manager/mod.rs +++ b/src-tauri/src/core/manager/mod.rs @@ -5,8 +5,8 @@ mod state; use anyhow::Result; use arc_swap::{ArcSwap, ArcSwapOption}; use std::{fmt, sync::Arc, time::Instant}; +use tauri_plugin_shell::process::CommandChild; -use crate::process::CommandChildGuard; use crate::singleton_lazy; #[derive(Debug, serde::Serialize, PartialEq, Eq)] @@ -35,7 +35,7 @@ pub struct CoreManager { #[derive(Debug)] struct State { running_mode: ArcSwap, - child_sidecar: ArcSwapOption, + child_sidecar: ArcSwapOption, } impl Default for State { @@ -61,7 +61,7 @@ impl CoreManager { Arc::clone(&self.state.load().running_mode.load()) } - pub fn take_child_sidecar(&self) -> Option { + pub fn take_child_sidecar(&self) -> Option { self.state .load() .child_sidecar @@ -78,7 +78,7 @@ impl CoreManager { state.running_mode.store(Arc::new(mode)); } - pub fn set_running_child_sidecar(&self, child: CommandChildGuard) { + pub fn set_running_child_sidecar(&self, child: CommandChild) { let state = self.state.load(); state.child_sidecar.store(Some(Arc::new(child))); } diff --git a/src-tauri/src/core/manager/state.rs b/src-tauri/src/core/manager/state.rs index f38c6582b..897383c25 100644 --- a/src-tauri/src/core/manager/state.rs +++ b/src-tauri/src/core/manager/state.rs @@ -4,7 +4,6 @@ use crate::{ config::Config, core::{handle, logger::CLASH_LOGGER, service}, logging, - process::CommandChildGuard, utils::{dirs, init::sidecar_writer}, }; use anyhow::Result; @@ -46,7 +45,7 @@ impl CoreManager { let pid = child.pid(); logging!(trace, Type::Core, "Sidecar started with PID: {}", pid); - self.set_running_child_sidecar(CommandChildGuard::new(child)); + self.set_running_child_sidecar(child); self.set_running_mode(RunningMode::Sidecar); let shared_writer: SharedWriter = @@ -100,8 +99,14 @@ impl CoreManager { } if let Some(child) = self.take_child_sidecar() { let pid = child.pid(); - drop(child); - logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid); + let result = child.kill(); + logging!( + trace, + Type::Core, + "Sidecar stopped (PID: {:?}, Result: {:?})", + pid, + result + ); } } diff --git a/src-tauri/src/process/guard.rs b/src-tauri/src/process/guard.rs deleted file mode 100644 index 73986cdb6..000000000 --- a/src-tauri/src/process/guard.rs +++ /dev/null @@ -1,30 +0,0 @@ -use tauri_plugin_shell::process::CommandChild; - -#[derive(Debug)] -pub struct CommandChildGuard(Option); - -impl Drop for CommandChildGuard { - #[inline] - fn drop(&mut self) { - self.kill(); - } -} - -impl CommandChildGuard { - #[inline] - pub const fn new(child: CommandChild) -> Self { - Self(Some(child)) - } - - #[inline] - pub fn kill(&mut self) { - if let Some(child) = self.0.take() { - let _ = child.kill(); - } - } - - #[inline] - pub fn pid(&self) -> Option { - self.0.as_ref().map(|c| c.pid()) - } -} diff --git a/src-tauri/src/process/mod.rs b/src-tauri/src/process/mod.rs index 77f99e78e..ac83ecc0d 100644 --- a/src-tauri/src/process/mod.rs +++ b/src-tauri/src/process/mod.rs @@ -1,4 +1,2 @@ mod async_handler; pub use async_handler::AsyncHandler; -mod guard; -pub use guard::CommandChildGuard;