Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib/server/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { externalProjectPathsAllowed, resolveContainedPath, sparkWorkspaceRoot }

export const MAX_OUTPUT_LENGTH = 5000;
export const COMMAND_TIMEOUT_MS = commandTimeoutMs();
const SIGTERM_GRACE_MS = 5000;

export interface CommandResult {
exitCode: number;
Expand Down Expand Up @@ -112,6 +113,19 @@ export function runCommand(
windowsHide: true
});

// Node sends SIGTERM when the spawn `timeout` fires. If the child ignores
// SIGTERM (npm/test shells with their own signal handlers, hung worker
// pools), `close` never fires and the API caller waits indefinitely.
// Schedule a SIGKILL escalation that fires only if the child is still
// running after the SIGTERM grace window.
const sigkillTimer = setTimeout(() => {
try {
child.kill('SIGKILL');
} catch {
// Child may already have exited after SIGTERM.
}
}, timeoutMs + SIGTERM_GRACE_MS);

child.stdout?.on('data', (data: Buffer) => {
stdout += data.toString();
});
Expand All @@ -121,6 +135,7 @@ export function runCommand(
});

child.on('close', (code) => {
clearTimeout(sigkillTimer);
if (!resolved) {
resolved = true;
res({
Expand All @@ -133,6 +148,7 @@ export function runCommand(
});

child.on('error', (err) => {
clearTimeout(sigkillTimer);
if (!resolved) {
resolved = true;
res({
Expand Down