fix: stop orphaned opencode processes on desktop quit

Exit the desktop app without waiting on background cleanup
Kill managed OpenCode by process group with a port fallback
Make OpenCode shutdown reuse the active shutdown promise
This commit is contained in:
Bohdan Triapitsyn
2026-06-03 14:51:17 +03:00
parent a649ba8df4
commit 2b098d36f5
5 changed files with 159 additions and 50 deletions
+18 -8
View File
@@ -152,6 +152,20 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
return;
}
const signalProcessTree = (signal) => {
if (process.platform !== 'win32') {
try {
process.kill(-pid, signal);
} catch {
}
}
try {
child.kill(signal);
} catch {
}
};
if (process.platform === 'win32') {
try {
child.kill();
@@ -188,19 +202,13 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
return;
}
try {
child.kill('SIGTERM');
} catch {
}
signalProcessTree('SIGTERM');
if (await waitForChildProcessClose(child, 2500)) {
return;
}
try {
child.kill('SIGKILL');
} catch {
}
signalProcessTree('SIGKILL');
await waitForChildProcessClose(child, 1000);
};
@@ -274,6 +282,7 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
const child = spawn(binary, args, {
cwd,
env: processEnv,
detached: process.platform !== 'win32',
windowsHide: true,
stdio: ['ignore', 'pipe', 'pipe'],
});
@@ -336,6 +345,7 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
return {
url,
pid: child.pid || null,
async close() {
await closeManagedOpenCodeChild(child);
},
@@ -29,7 +29,9 @@ export const createGracefulShutdownRuntime = (dependencies) => {
tunnelAuthController,
} = dependencies;
const gracefulShutdown = async (options = {}) => {
let shutdownPromise = null;
const runShutdown = async (options = {}) => {
if (getIsShuttingDown()) return;
setIsShuttingDown(true);
@@ -133,6 +135,12 @@ export const createGracefulShutdownRuntime = (dependencies) => {
}
};
const gracefulShutdown = (options = {}) => {
if (shutdownPromise) return shutdownPromise;
shutdownPromise = runShutdown(options);
return shutdownPromise;
};
return {
gracefulShutdown,
};