Refactor ShellExecutionService cleanup to use strategy pattern

This commit is contained in:
xuewenjie
2025-12-12 17:03:04 +08:00
parent 16939c0bc8
commit 574d89da14

View File

@@ -98,6 +98,48 @@ const getFullBufferText = (terminal: pkg.Terminal): string => {
return lines.join('\n').trimEnd(); return lines.join('\n').trimEnd();
}; };
interface ProcessCleanupStrategy {
killPty(pid: number, pty: ActivePty): void;
killChildProcesses(pids: Set<number>): void;
}
const windowsStrategy: ProcessCleanupStrategy = {
killPty: (_pid, pty) => {
pty.ptyProcess.kill();
},
killChildProcesses: (pids) => {
if (pids.size > 0) {
try {
const args = ['/f', '/t'];
for (const pid of pids) {
args.push('/pid', pid.toString());
}
spawnSync('taskkill', args);
} catch {
// ignore
}
}
},
};
const posixStrategy: ProcessCleanupStrategy = {
killPty: (pid, _pty) => {
process.kill(-pid, 'SIGKILL');
},
killChildProcesses: (pids) => {
for (const pid of pids) {
try {
process.kill(-pid, 'SIGKILL');
} catch {
// ignore
}
}
},
};
const cleanupStrategy =
os.platform() === 'win32' ? windowsStrategy : posixStrategy;
/** /**
* A centralized service for executing shell commands with robust process * A centralized service for executing shell commands with robust process
* management, cross-platform compatibility, and streaming output capabilities. * management, cross-platform compatibility, and streaming output capabilities.
@@ -112,38 +154,14 @@ export class ShellExecutionService {
// Cleanup PTYs // Cleanup PTYs
for (const [pid, pty] of this.activePtys) { for (const [pid, pty] of this.activePtys) {
try { try {
if (os.platform() === 'win32') { cleanupStrategy.killPty(pid, pty);
pty.ptyProcess.kill();
} else {
process.kill(-pid, 'SIGKILL');
}
} catch { } catch {
// ignore // ignore
} }
} }
// Cleanup child processes // Cleanup child processes
if (os.platform() === 'win32') { cleanupStrategy.killChildProcesses(this.activeChildProcesses);
if (this.activeChildProcesses.size > 0) {
try {
const args = ['/f', '/t'];
for (const pid of this.activeChildProcesses) {
args.push('/pid', pid.toString());
}
spawnSync('taskkill', args);
} catch {
// ignore
}
}
} else {
for (const pid of this.activeChildProcesses) {
try {
process.kill(-pid, 'SIGKILL');
} catch {
// ignore
}
}
}
} }
static { static {