mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-12 22:14:22 +00:00
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:
1 parent
4b1fcacf30
commit
90b4627740
3 files changed
+25
-29
No files matched your search
@@ -6,20 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { spawn, ChildProcess } from 'child_process';
|
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 { ok, fail, info, warn } from '../../utils/ui';
|
||||||
|
import { killWithEscalation } from '../../utils/process-utils';
|
||||||
import { tryKiroImport } from './kiro-import';
|
import { tryKiroImport } from './kiro-import';
|
||||||
import { CLIProxyProvider } from '../types';
|
import { CLIProxyProvider } from '../types';
|
||||||
import { AccountInfo } from '../account-manager';
|
import { AccountInfo } from '../account-manager';
|
||||||
@@ -371,9 +359,9 @@ export function executeOAuthProcess(options: OAuthProcessOptions): Promise<Accou
|
|||||||
|
|
||||||
// Listen for external cancel signal
|
// Listen for external cancel signal
|
||||||
const handleCancel = (cancelledSessionId: string) => {
|
const handleCancel = (cancelledSessionId: string) => {
|
||||||
if (cancelledSessionId === state.sessionId && authProcess && !authProcess.killed) {
|
if (cancelledSessionId === state.sessionId && authProcess && authProcess.exitCode === null) {
|
||||||
log('Session cancelled externally');
|
log('Session cancelled externally');
|
||||||
authProcess.kill('SIGTERM');
|
killWithEscalation(authProcess);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
authSessionEvents.on('session:cancelled', handleCancel);
|
authSessionEvents.on('session:cancelled', handleCancel);
|
||||||
|
|||||||
@@ -5,21 +5,9 @@
|
|||||||
* Spawns claude with -p flag for single-turn execution
|
* Spawns claude with -p flag for single-turn execution
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { spawn, ChildProcess } from 'child_process';
|
import { spawn } 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 * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { killWithEscalation } from '../utils/process-utils';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { SessionManager } from './session-manager';
|
import { SessionManager } from './session-manager';
|
||||||
import { SettingsParser } from './settings-parser';
|
import { SettingsParser } from './settings-parser';
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user