|
1 | 1 | import type { CliRenderer } from '@opentui/core' |
2 | 2 |
|
3 | | -/** |
4 | | - * Global reference to the CLI renderer for cleanup on exit. |
5 | | - * This allows the exit handler to properly destroy the renderer, |
6 | | - * which resets terminal state (mouse tracking, focus reporting, raw mode, etc.) |
7 | | - */ |
8 | | -let registeredRenderer: CliRenderer | null = null |
| 3 | +let renderer: CliRenderer | null = null |
| 4 | +let handlersInstalled = false |
9 | 5 |
|
10 | 6 | /** |
11 | | - * Register the renderer for cleanup on exit. |
12 | | - * Call this after creating the renderer in index.tsx. |
| 7 | + * Clean up the renderer by calling destroy(). |
| 8 | + * This resets terminal state to prevent garbled output after exit. |
13 | 9 | */ |
14 | | -export function registerRendererForCleanup(renderer: CliRenderer): void { |
15 | | - registeredRenderer = renderer |
| 10 | +function cleanup(): void { |
| 11 | + if (renderer && !renderer.isDestroyed) { |
| 12 | + try { |
| 13 | + renderer.destroy() |
| 14 | + } catch { |
| 15 | + // Ignore errors during cleanup - we're exiting anyway |
| 16 | + } |
| 17 | + renderer = null |
| 18 | + } |
16 | 19 | } |
17 | 20 |
|
18 | 21 | /** |
19 | | - * Cleanup the renderer by calling destroy(). |
20 | | - * This resets terminal state to prevent garbled output after exit. |
21 | | - * Should be called before process.exit() in exit handlers. |
| 22 | + * Install process-level signal handlers to ensure terminal cleanup on all exit scenarios. |
| 23 | + * Call this once after creating the renderer in index.tsx. |
| 24 | + * |
| 25 | + * This handles: |
| 26 | + * - SIGTERM (kill) |
| 27 | + * - SIGHUP (terminal hangup) |
| 28 | + * - SIGINT (Ctrl+C) |
| 29 | + * - beforeExit / exit events |
| 30 | + * - uncaughtException / unhandledRejection |
| 31 | + * |
| 32 | + * Note: SIGKILL cannot be caught - it's an immediate termination signal. |
22 | 33 | */ |
23 | | -export function cleanupRenderer(): void { |
24 | | - if (registeredRenderer && !registeredRenderer.isDestroyed) { |
| 34 | +export function installProcessCleanupHandlers(cliRenderer: CliRenderer): void { |
| 35 | + if (handlersInstalled) return |
| 36 | + handlersInstalled = true |
| 37 | + renderer = cliRenderer |
| 38 | + |
| 39 | + const cleanupAndExit = (exitCode: number) => { |
| 40 | + cleanup() |
| 41 | + process.exit(exitCode) |
| 42 | + } |
| 43 | + |
| 44 | + // SIGTERM - Default kill signal (e.g., `kill <pid>`) |
| 45 | + process.on('SIGTERM', () => { |
| 46 | + cleanupAndExit(0) |
| 47 | + }) |
| 48 | + |
| 49 | + // SIGHUP - Terminal hangup (e.g., closing the terminal window) |
| 50 | + process.on('SIGHUP', () => { |
| 51 | + cleanupAndExit(0) |
| 52 | + }) |
| 53 | + |
| 54 | + // SIGINT - Ctrl+C |
| 55 | + process.on('SIGINT', () => { |
| 56 | + cleanupAndExit(0) |
| 57 | + }) |
| 58 | + |
| 59 | + // beforeExit - Called when the event loop is empty and about to exit |
| 60 | + process.on('beforeExit', () => { |
| 61 | + cleanup() |
| 62 | + }) |
| 63 | + |
| 64 | + // exit - Last chance to run synchronous cleanup code |
| 65 | + process.on('exit', () => { |
| 66 | + cleanup() |
| 67 | + }) |
| 68 | + |
| 69 | + // uncaughtException - Safety net for unhandled errors |
| 70 | + process.on('uncaughtException', (error) => { |
25 | 71 | try { |
26 | | - registeredRenderer.destroy() |
| 72 | + console.error('Uncaught exception:', error) |
27 | 73 | } catch { |
28 | | - // Ignore errors during cleanup - we're exiting anyway |
| 74 | + // Ignore logging errors |
29 | 75 | } |
30 | | - registeredRenderer = null |
31 | | - } |
| 76 | + cleanupAndExit(1) |
| 77 | + }) |
| 78 | + |
| 79 | + // unhandledRejection - Safety net for unhandled promise rejections |
| 80 | + process.on('unhandledRejection', (reason) => { |
| 81 | + try { |
| 82 | + console.error('Unhandled rejection:', reason) |
| 83 | + } catch { |
| 84 | + // Ignore logging errors |
| 85 | + } |
| 86 | + cleanupAndExit(1) |
| 87 | + }) |
32 | 88 | } |
0 commit comments