Elffuss BraidHub

kikocis/continue public

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

fix: SIGINT handler logic to run in TUI only
Changeset85ccad1ce18a788583fc0837d3802049cf514a98e27236a2438ef4ee81b2aeb9
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-08 16:52 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-08 16:52 -07:00
Parents66ccb9456799, a7ece0164f45

Browse the files at this changeset

2 files changed

modified extensions/cli/src/index.ts +36 −35
@@ -47,6 +47,42 @@
4747 exitMessageCallback = callback;
4848 }
4949
50+// Sets up SIGINT handler that requires double Ctrl+C within 1 second to exit
51+export function enableSigintHandler() {
52+ // Remove all existing SIGINT listeners first
53+ process.removeAllListeners("SIGINT");
54+
55+ process.on("SIGINT", async () => {
56+ const now = Date.now();
57+ const timeSinceLastCtrlC = now - lastCtrlCTime;
58+
59+ if (timeSinceLastCtrlC <= 1000 && lastCtrlCTime !== 0) {
60+ // Second Ctrl+C within 1 second - exit
61+ showExitMessage = false;
62+ if (tuiUnmount) {
63+ tuiUnmount();
64+ }
65+ await sentryService.flush();
66+ process.exit(0);
67+ } else {
68+ // First Ctrl+C or too much time elapsed - show exit message
69+ lastCtrlCTime = now;
70+ showExitMessage = true;
71+ if (exitMessageCallback) {
72+ exitMessageCallback();
73+ }
74+
75+ // Hide message after 1 second
76+ setTimeout(() => {
77+ showExitMessage = false;
78+ if (exitMessageCallback) {
79+ exitMessageCallback();
80+ }
81+ }, 1000);
82+ }
83+ });
84+}
85+
5086 // Check if "ctrl+c to exit" message should be displayed
5187 export function shouldShowExitMessage(): boolean {
5288 return showExitMessage;
@@ -294,41 +330,6 @@
294330 process.exit(1);
295331 }
296332
297-// Graceful shutdown with two-stage Ctrl+C exit
298-
299-// Remove all existing SIGINT listeners first
300-process.removeAllListeners("SIGINT");
301-
302-process.on("SIGINT", async () => {
303- const now = Date.now();
304- const timeSinceLastCtrlC = now - lastCtrlCTime;
305-
306- if (timeSinceLastCtrlC <= 1000 && lastCtrlCTime !== 0) {
307- // Second Ctrl+C within 1 second - exit
308- showExitMessage = false;
309- if (tuiUnmount) {
310- tuiUnmount();
311- }
312- await sentryService.flush();
313- process.exit(0);
314- } else {
315- // First Ctrl+C or too much time elapsed - show exit message
316- lastCtrlCTime = now;
317- showExitMessage = true;
318- if (exitMessageCallback) {
319- exitMessageCallback();
320- }
321-
322- // Hide message after 1 second
323- setTimeout(() => {
324- showExitMessage = false;
325- if (exitMessageCallback) {
326- exitMessageCallback();
327- }
328- }, 1000);
329- }
330-});
331-
332333 process.on("SIGTERM", async () => {
333334 await sentryService.flush();
334335 process.exit(0);
modified extensions/cli/src/ui/index.ts +4 −1
@@ -1,7 +1,7 @@
11 import { render, RenderOptions } from "ink";
22 import React from "react";
33
4-import { setTUIUnmount } from "../index.js";
4+import { enableSigintHandler, setTUIUnmount } from "../index.js";
55 import { PermissionMode } from "../permissions/types.js";
66 import { initializeServices } from "../services/index.js";
77 import { ServiceContainerProvider } from "../services/ServiceContainerContext.js";
@@ -79,6 +79,9 @@
7979 // Register unmount function with main process for two-stage Ctrl+C exit
8080 setTUIUnmount(unmount);
8181
82+ // Enable the two-stage SIGINT handler for TUI mode
83+ enableSigintHandler();
84+
8285 return { unmount };
8386 }
8487