From bff3aedf866ea20bd47c9a80f13bdb4fe8a040c2 Mon Sep 17 00:00:00 2001 From: xmk23333 Date: Tue, 6 Jan 2026 02:34:42 +0800 Subject: [PATCH] fix: kill old mihomo processes before copying files to avoid EBUSY --- src/main/utils/init.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index 03d1187..9ef030a 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -149,14 +149,49 @@ async function initConfig(): Promise { } } +async function killOldMihomoProcesses(): Promise { + const execPromise = promisify(exec) + try { + const { stdout } = await execPromise( + 'powershell -NoProfile -Command "Get-Process | Where-Object {$_.ProcessName -like \'*mihomo*\'} | Select-Object Id | ConvertTo-Json"', + { encoding: 'utf8' } + ) + + if (!stdout.trim()) return + + const processes = JSON.parse(stdout) + const processArray = Array.isArray(processes) ? processes : [processes] + + for (const proc of processArray) { + const pid = proc.Id + if (pid && pid !== process.pid) { + try { + process.kill(pid, 'SIGTERM') + await initLogger.info(`Terminated old mihomo process ${pid}`) + } catch { + // 进程可能退出 + } + } + } + + // 等待进程完全退出 + await new Promise((resolve) => setTimeout(resolve, 500)) + } catch { + } +} + async function initFiles(): Promise { + // 结束旧 mihomo 进程 + if (process.platform === 'win32') { + await killOldMihomoProcesses() + } + const copy = async (file: string): Promise => { const targetPath = path.join(mihomoWorkDir(), file) const testTargetPath = path.join(mihomoTestDir(), file) const sourcePath = path.join(resourcesFilesDir(), file) try { - // 检查是否需要复制 if (existsSync(sourcePath)) { const shouldCopyToWork = !existsSync(targetPath) || (await isSourceNewer(sourcePath, targetPath))