diff options
| author | Olcan <[email protected]> | 2025-05-30 01:35:03 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-30 01:35:03 -0700 |
| commit | a3b557222a95965de39920922df21439d69def57 (patch) | |
| tree | b2f5fd038a10f0ea659829658d2355c6bbe20941 /packages/cli/src/ui/hooks/shellCommandProcessor.ts | |
| parent | 094b9dc474fd8b99813acfa53e1192058314e9e4 (diff) | |
tweaks to shell abort logic based on feedback (#618)
Diffstat (limited to 'packages/cli/src/ui/hooks/shellCommandProcessor.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/shellCommandProcessor.ts | 44 |
1 files changed, 13 insertions, 31 deletions
diff --git a/packages/cli/src/ui/hooks/shellCommandProcessor.ts b/packages/cli/src/ui/hooks/shellCommandProcessor.ts index d6c837d1..1325fd1d 100644 --- a/packages/cli/src/ui/hooks/shellCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/shellCommandProcessor.ts @@ -37,7 +37,7 @@ export const useShellCommandProcessor = ( * @returns True if the query was handled as a shell command, false otherwise. */ const handleShellCommand = useCallback( - (rawQuery: PartListUnion, signal?: AbortSignal): boolean => { + (rawQuery: PartListUnion, abortSignal: AbortSignal): boolean => { if (typeof rawQuery !== 'string') { return false; } @@ -149,27 +149,19 @@ export const useShellCommandProcessor = ( error = err; }); - const abortHandler = () => { + const abortHandler = async () => { if (child.pid && !exited) { onDebugMessage( `Aborting shell command (PID: ${child.pid}) due to signal.`, ); try { // attempt to SIGTERM process group (negative PID) - // if SIGTERM fails after 200ms, attempt SIGKILL + // fall back to SIGKILL (to group) after 200ms process.kill(-child.pid, 'SIGTERM'); - setTimeout(() => { - // if SIGTERM fails, attempt SIGKILL - try { - if (child.pid && !exited) { - process.kill(-child.pid, 'SIGKILL'); - } - } catch (_e) { - console.error( - `failed to kill shell process ${child.pid}: ${_e}`, - ); - } - }, 200); + await new Promise((resolve) => setTimeout(resolve, 200)); + if (child.pid && !exited) { + process.kill(-child.pid, 'SIGKILL'); + } } catch (_e) { // if group kill fails, fall back to killing just the main process try { @@ -185,20 +177,11 @@ export const useShellCommandProcessor = ( } }; - if (signal) { - if (signal.aborted) { - abortHandler(); - // No need to add listener if already aborted - } else { - signal.addEventListener('abort', abortHandler, { once: true }); - } - } + abortSignal.addEventListener('abort', abortHandler, { once: true }); - child.on('exit', (code, processSignal) => { + child.on('exit', (code, signal) => { exited = true; - if (signal) { - signal.removeEventListener('abort', abortHandler); - } + abortSignal.removeEventListener('abort', abortHandler); setPendingHistoryItem(null); output = output.trim() || '(Command produced no output)'; if (error) { @@ -207,7 +190,7 @@ export const useShellCommandProcessor = ( } else if (code !== null && code !== 0) { const text = `Command exited with code ${code}\n${output}`; addItemToHistory({ type: 'error', text }, userMessageTimestamp); - } else if (signal?.aborted) { + } else if (abortSignal.aborted) { addItemToHistory( { type: 'info', @@ -215,9 +198,8 @@ export const useShellCommandProcessor = ( }, userMessageTimestamp, ); - } else if (processSignal) { - const text = `Command terminated with signal ${processSignal} -${output}`; + } else if (signal) { + const text = `Command terminated with signal ${signal}.\n${output}`; addItemToHistory({ type: 'error', text }, userMessageTimestamp); } else { addItemToHistory( |
