Skip to content

Commit 898cf22

Browse files
committed
perf: use cmd+netstat instead of PowerShell for port cleanup on Windows
PowerShell cold start adds 1-2s per call; called twice on launch = 2-4s wasted.
1 parent 3c2eba3 commit 898cf22

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

apps/desktop/electron/process-manager.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,35 @@ export class ProcessManager {
5858
try {
5959
let cmd: string;
6060
if (process.platform === 'win32') {
61-
cmd = `powershell -Command "Get-NetTCPConnection -LocalPort ${port} -ErrorAction SilentlyContinue | ForEach-Object { $_.OwningProcess }"`;
61+
// Use cmd+netstat instead of PowerShell to avoid 1-2s cold start delay
62+
cmd = `cmd /c "netstat -ano | findstr :${port} | findstr LISTENING"`;
6263
} else {
6364
cmd = `lsof -ti tcp:${port}`;
6465
}
6566
const result = execSync(cmd, { encoding: 'utf-8', timeout: 3000 }).trim();
6667
if (result) {
67-
for (const pid of result.split('\n')) {
68-
const p = parseInt(pid.trim());
69-
if (p > 0) {
70-
try {
71-
if (process.platform === 'win32') {
72-
execSync(`taskkill /PID ${p} /F`, { encoding: 'utf-8' });
73-
} else {
74-
process.kill(p, 'SIGKILL');
75-
}
76-
this.logger.info(`Killed stale process ${p} on port ${port}`);
77-
} catch { /* already dead */ }
68+
const pids = new Set<number>();
69+
for (const line of result.split('\n')) {
70+
if (process.platform === 'win32') {
71+
// netstat -ano output: " TCP 0.0.0.0:18080 0.0.0.0:0 LISTENING 1234"
72+
const parts = line.trim().split(/\s+/);
73+
const p = parseInt(parts[parts.length - 1]);
74+
if (p > 0) pids.add(p);
75+
} else {
76+
const p = parseInt(line.trim());
77+
if (p > 0) pids.add(p);
7878
}
7979
}
80+
for (const p of pids) {
81+
try {
82+
if (process.platform === 'win32') {
83+
execSync(`taskkill /PID ${p} /F`, { encoding: 'utf-8' });
84+
} else {
85+
process.kill(p, 'SIGKILL');
86+
}
87+
this.logger.info(`Killed stale process ${p} on port ${port}`);
88+
} catch { /* already dead */ }
89+
}
8090
}
8191
} catch { /* no process on port */ }
8292
}

0 commit comments

Comments
 (0)