fix: 修复关闭软件后 OpenCode 进程泄漏问题
- 使用 taskkill /F /T 强制终止进程树 - 在 before-quit 中 await 等待服务停止完成 - 修复 stop 方法中可能的空指针问题
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { spawn, ChildProcess } from 'child_process';
|
||||
import { spawn, exec, ChildProcess } from 'child_process';
|
||||
import log from 'electron-log';
|
||||
|
||||
const OPENCODE_PORT = 4096;
|
||||
@@ -150,21 +150,33 @@ class OpenCodeService {
|
||||
try {
|
||||
log.info('[OpenCodeService] Stopping...');
|
||||
|
||||
this.process.kill('SIGTERM');
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(resolve, 1000);
|
||||
});
|
||||
|
||||
if (this.process && !this.process.killed) {
|
||||
this.process.kill('SIGKILL');
|
||||
}
|
||||
|
||||
const pid = this.process.pid;
|
||||
this.process = null;
|
||||
this._isRunning = false;
|
||||
this.restartAttempts = 0;
|
||||
|
||||
log.info('[OpenCodeService] Stopped');
|
||||
if (pid && process.platform === 'win32') {
|
||||
return new Promise((resolve) => {
|
||||
exec(`taskkill /F /T /PID ${pid}`, (error) => {
|
||||
if (error) {
|
||||
log.warn('[OpenCodeService] taskkill failed, process may already be dead:', error.message);
|
||||
}
|
||||
this.restartAttempts = 0;
|
||||
log.info('[OpenCodeService] Stopped');
|
||||
resolve({ success: true });
|
||||
});
|
||||
});
|
||||
} else if (pid) {
|
||||
this.process?.kill('SIGTERM');
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, 1000));
|
||||
if (this.process && !this.process.killed) {
|
||||
this.process.kill('SIGKILL');
|
||||
}
|
||||
this.restartAttempts = 0;
|
||||
log.info('[OpenCodeService] Stopped');
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
this.restartAttempts = 0;
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
log.error('[OpenCodeService] Failed to stop:', error);
|
||||
|
||||
Reference in New Issue
Block a user