refactor(utils): extract killWithEscalation to shared process-utils

- Move killWithEscalation to src/utils/process-utils.ts (DRY)
- Fix handleCancel to use exitCode === null and killWithEscalation
- Fix import ordering in both oauth-process.ts and headless-executor.ts
This commit is contained in:
Tam Nhu Tran
2026-02-11 02:08:12 +07:00
parent 4b1fcacf30
commit 90b4627740
3 changed files with 25 additions and 29 deletions
+3 -15
View File
@@ -6,20 +6,8 @@
*/
import { spawn, ChildProcess } from 'child_process';
/**
* Kill process with SIGTERM, escalating to SIGKILL if it doesn't exit
*/
function killWithEscalation(proc: ChildProcess, gracePeriodMs = 3000): void {
proc.kill('SIGTERM');
const timer = setTimeout(() => {
if (proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, gracePeriodMs);
proc.once('exit', () => clearTimeout(timer));
}
import { ok, fail, info, warn } from '../../utils/ui';
import { killWithEscalation } from '../../utils/process-utils';
import { tryKiroImport } from './kiro-import';
import { CLIProxyProvider } from '../types';
import { AccountInfo } from '../account-manager';
@@ -371,9 +359,9 @@ export function executeOAuthProcess(options: OAuthProcessOptions): Promise<Accou
// Listen for external cancel signal
const handleCancel = (cancelledSessionId: string) => {
if (cancelledSessionId === state.sessionId && authProcess && !authProcess.killed) {
if (cancelledSessionId === state.sessionId && authProcess && authProcess.exitCode === null) {
log('Session cancelled externally');
authProcess.kill('SIGTERM');
killWithEscalation(authProcess);
}
};
authSessionEvents.on('session:cancelled', handleCancel);
+2 -14
View File
@@ -5,21 +5,9 @@
* Spawns claude with -p flag for single-turn execution
*/
import { spawn, ChildProcess } from 'child_process';
/**
* Kill process with SIGTERM, escalating to SIGKILL if it doesn't exit
*/
function killWithEscalation(proc: ChildProcess, gracePeriodMs = 3000): void {
proc.kill('SIGTERM');
const timer = setTimeout(() => {
if (proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, gracePeriodMs);
proc.once('exit', () => clearTimeout(timer));
}
import { spawn } from 'child_process';
import * as path from 'path';
import { killWithEscalation } from '../utils/process-utils';
import * as fs from 'fs';
import { SessionManager } from './session-manager';
import { SettingsParser } from './settings-parser';
+20
View File
@@ -0,0 +1,20 @@
/**
* Process management utilities
*/
import { ChildProcess } from 'child_process';
/**
* Kill process with SIGTERM, escalating to SIGKILL if it doesn't exit.
* Uses exitCode === null (not proc.killed) to check if process is still running,
* since proc.killed only indicates a signal was sent, not that the process exited.
*/
export function killWithEscalation(proc: ChildProcess, gracePeriodMs = 3000): void {
proc.kill('SIGTERM');
const timer = setTimeout(() => {
if (proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, gracePeriodMs);
proc.once('exit', () => clearTimeout(timer));
}