TUI: guard SIGTERM shutdown against setRawMode EBADF

This commit is contained in:
Vignesh Natarajan
2026-02-28 14:56:01 -08:00
parent 2050fd7539
commit fca0467082
3 changed files with 61 additions and 1 deletions

View File

@@ -246,6 +246,30 @@ export function createBackspaceDeduper(params?: { dedupeWindowMs?: number; now?:
};
}
export function isIgnorableTuiStopError(error: unknown): boolean {
if (!error || typeof error !== "object") {
return false;
}
const err = error as { code?: unknown; syscall?: unknown; message?: unknown };
const code = typeof err.code === "string" ? err.code : "";
const syscall = typeof err.syscall === "string" ? err.syscall : "";
const message = typeof err.message === "string" ? err.message : "";
if (code === "EBADF" && syscall === "setRawMode") {
return true;
}
return /setRawMode/i.test(message) && /EBADF/i.test(message);
}
export function stopTuiSafely(stop: () => void): void {
try {
stop();
} catch (error) {
if (!isIgnorableTuiStopError(error)) {
throw error;
}
}
}
type CtrlCAction = "clear" | "warn" | "exit";
export function resolveCtrlCAction(params: {
@@ -770,7 +794,7 @@ export async function runTui(opts: TuiOptions) {
}
exitRequested = true;
client.stop();
tui.stop();
stopTuiSafely(() => tui.stop());
process.exit(0);
};