Merge pull request #7638 from continuedev/tingwai/con-3836-ctrl-c-in-tui-only

fix: SIGINT handler logic to run in TUI only
This commit is contained in:
Dallin Romney 2025-09-08 16:52:37 -07:00 • committed by GitHub
commit 2837f3d33c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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 };
}