From e6b1b2fcbf95f409acef0c0df200a7841f52783a Mon Sep 17 00:00:00 2001 From: sw3205933776 <3205933776@qq.com> Date: Thu, 14 Aug 2025 10:35:06 +0800 Subject: [PATCH 1/2] fix: unable to kill Python process on specified port --- electron/main/init.ts | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/electron/main/init.ts b/electron/main/init.ts index 70dbb7824..7283e592b 100644 --- a/electron/main/init.ts +++ b/electron/main/init.ts @@ -426,25 +426,33 @@ function checkPortAvailable(port: number): Promise { export async function killProcessOnPort(port: number): Promise { try { const platform = process.platform; - let command: string; if (platform === 'win32') { - // Windows command to find and kill process - command = ` - for /f "tokens=5" %%a in ('netstat -ano ^| findstr :${port}') do ( - echo Killing PID: %%a - taskkill /F /PID %%a - ) - `; - } else if (platform === 'darwin') { - // macOS command - command = `lsof -ti:${port} | xargs kill -9 2>/dev/null || true`; - } else { - // Linux command - command = `fuser -k ${port}/tcp 2>/dev/null || true`; + // 1. get pid of process listen on port + const { stdout: netstatOut } = await execAsync(`netstat -ano | findstr LISTENING | findstr :${port}`); + const lines = netstatOut.trim().split(/\r?\n/).filter(Boolean); + if (lines.length === 0) { + console.log(`no process listen on port ${port}`); + return true; + } + + // get pid from last field + const pid = lines[0].trim().split(/\s+/).pop(); + if (!pid) { + console.log(`no pid listen on port ${port}`); + return false; + } + + console.log(`Killing PID: ${pid}`); + await execAsync(`taskkill /F /PID ${pid}`); + } + else if (platform === 'darwin') { + await execAsync(`lsof -ti:${port} | xargs kill -9 2>/dev/null || true`); + } + else { + await execAsync(`fuser -k ${port}/tcp 2>/dev/null || true`); } - await execAsync(command); // Wait a bit for the process to be killed await new Promise(resolve => setTimeout(resolve, 500)); From e2feef5df9e86c9a6efe57a5708c61ea33fc5ba7 Mon Sep 17 00:00:00 2001 From: Wendong-Fan Date: Thu, 14 Aug 2025 20:01:25 +0800 Subject: [PATCH 2/2] minor update --- electron/main/init.ts | 4 ++-- src/pages/Home.tsx | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/electron/main/init.ts b/electron/main/init.ts index 7283e592b..ac364dbf8 100644 --- a/electron/main/init.ts +++ b/electron/main/init.ts @@ -438,8 +438,8 @@ export async function killProcessOnPort(port: number): Promise { // get pid from last field const pid = lines[0].trim().split(/\s+/).pop(); - if (!pid) { - console.log(`no pid listen on port ${port}`); + if (!pid || isNaN(Number(pid))) { + console.log(`Invalid PID extracted for port ${port}: ${pid}`); return false; } diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 72b2b4cf0..eb0521ffe 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -159,7 +159,7 @@ export default function Home() {
- + {/* left transparent area */}