fix: SIGINT handler logic to run in TUI only

Fixes issue of user not being able to ctrl-c while onboarding
This commit is contained in:
Ting-Wai To 2025-09-08 11:42:40 -07:00
parent 1218c14f89
commit 705f99efff
2 changed files with 40 additions and 36 deletions

View file

@ -47,6 +47,42 @@ export function setExitMessageCallback(callback: () => void) {
exitMessageCallback = callback;
}
// Sets up SIGINT handler that requires double Ctrl+C within 1 second to exit
export function enableSigintHandler() {
// Remove all existing SIGINT listeners first
process.removeAllListeners("SIGINT");
process.on("SIGINT", async () => {
const now = Date.now();
const timeSinceLastCtrlC = now - lastCtrlCTime;
if (timeSinceLastCtrlC <= 1000 && lastCtrlCTime !== 0) {
// Second Ctrl+C within 1 second - exit
showExitMessage = false;
if (tuiUnmount) {
tuiUnmount();
}
await sentryService.flush();
process.exit(0);
} else {
// First Ctrl+C or too much time elapsed - show exit message
lastCtrlCTime = now;
showExitMessage = true;
if (exitMessageCallback) {
exitMessageCallback();
}
// Hide message after 1 second
setTimeout(() => {
showExitMessage = false;
if (exitMessageCallback) {
exitMessageCallback();
}
}, 1000);
}
});
}
// Check if "ctrl+c to exit" message should be displayed
export function shouldShowExitMessage(): boolean {
return showExitMessage;
@ -294,41 +330,6 @@ try {
process.exit(1);
}
// Graceful shutdown with two-stage Ctrl+C exit
// Remove all existing SIGINT listeners first
process.removeAllListeners("SIGINT");
process.on("SIGINT", async () => {
const now = Date.now();
const timeSinceLastCtrlC = now - lastCtrlCTime;
if (timeSinceLastCtrlC <= 1000 && lastCtrlCTime !== 0) {
// Second Ctrl+C within 1 second - exit
showExitMessage = false;
if (tuiUnmount) {
tuiUnmount();
}
await sentryService.flush();
process.exit(0);
} else {
// First Ctrl+C or too much time elapsed - show exit message
lastCtrlCTime = now;
showExitMessage = true;
if (exitMessageCallback) {
exitMessageCallback();
}
// Hide message after 1 second
setTimeout(() => {
showExitMessage = false;
if (exitMessageCallback) {
exitMessageCallback();
}
}, 1000);
}
});
process.on("SIGTERM", async () => {
await sentryService.flush();
process.exit(0);

View file

@ -1,7 +1,7 @@
import { render, RenderOptions } from "ink";
import React from "react";
import { setTUIUnmount } from "../index.js";
import { enableSigintHandler, setTUIUnmount } from "../index.js";
import { PermissionMode } from "../permissions/types.js";
import { initializeServices } from "../services/index.js";
import { ServiceContainerProvider } from "../services/ServiceContainerContext.js";
@ -79,6 +79,9 @@ export async function startTUIChat(
// Register unmount function with main process for two-stage Ctrl+C exit
setTUIUnmount(unmount);
// Enable the two-stage SIGINT handler for TUI mode
enableSigintHandler();
return { unmount };
}