From f709117cc40a9c2a96a9008ada04c8cadcc819a9 Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Thu, 17 Feb 2022 02:10:25 +0800
Subject: [PATCH] feat: kill sidecars when update app

---
 src-tauri/src/cmds.rs                   | 8 +++++++-
 src-tauri/src/main.rs                   | 1 +
 src/components/layout/update-dialog.tsx | 2 ++
 src/services/cmds.ts                    | 4 ++++
 4 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs
index 577036f..4cd15e7 100644
--- a/src-tauri/src/cmds.rs
+++ b/src-tauri/src/cmds.rs
@@ -5,7 +5,7 @@ use crate::{
 };
 use serde_yaml::Mapping;
 use std::process::Command;
-use tauri::State;
+use tauri::{api, State};
 
 /// get all profiles from `profiles.yaml`
 /// do not acquire the lock of ProfileLock
@@ -273,6 +273,12 @@ pub async fn patch_verge_config(
   verge.patch_config(payload)
 }
 
+/// kill all sidecars when update app
+#[tauri::command]
+pub fn kill_sidecars() {
+  api::process::kill_children();
+}
+
 /// open app config dir
 #[tauri::command]
 pub fn open_app_dir() -> Result<(), String> {
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 31dcdc8..e577ab9 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -74,6 +74,7 @@ fn main() -> std::io::Result<()> {
       cmds::restart_sidecar,
       cmds::get_sys_proxy,
       cmds::get_cur_proxy,
+      cmds::kill_sidecars,
       cmds::open_app_dir,
       cmds::open_logs_dir,
       // clash
diff --git a/src/components/layout/update-dialog.tsx b/src/components/layout/update-dialog.tsx
index 1881148..3111982 100644
--- a/src/components/layout/update-dialog.tsx
+++ b/src/components/layout/update-dialog.tsx
@@ -10,6 +10,7 @@ import {
   DialogContentText,
   DialogTitle,
 } from "@mui/material";
+import { killSidecars } from "../../services/cmds";
 
 interface Props {
   open: boolean;
@@ -31,6 +32,7 @@ const UpdateDialog = (props: Props) => {
     try {
       setUploading(true);
       uploadingState = true;
+      await killSidecars();
       await installUpdate();
       await relaunch();
     } catch (error) {
diff --git a/src/services/cmds.ts b/src/services/cmds.ts
index 9ef9c13..b8d9d4a 100644
--- a/src/services/cmds.ts
+++ b/src/services/cmds.ts
@@ -64,6 +64,10 @@ export async function getSystemProxy() {
   return invoke<any>("get_sys_proxy");
 }
 
+export async function killSidecars() {
+  return invoke<any>("kill_sidecars");
+}
+
 export async function openAppDir() {
   return invoke<void>("open_app_dir");
 }
-- 
GitLab