Own process termination on detached throws and OS signals - #406
Merged
Conversation
A throw that escapes runTUI's own try/catch (e.g. inside a fire-and-forget void call) only reached the top-level uncaughtException/unhandledRejection handler, which had no way to call runTUI's terminal-restore routine since it lives as a closure bound only once the OpenTUI host mounts. The process exited with the terminal left in the alternate screen and raw mode still on. A module-level slot mirroring the existing active-run.ts pattern lets the handler reach it.
Corbits registered no signal handlers of its own. OpenTUI's vendored renderer restores the terminal on these signals when a TUI is mounted, but never calls process.exit, so the process (and the run's state on disk) was left hanging indefinitely after an external kill. Outside an interactive session (exec mode, or before a host mounts) nothing handled the signal at all, so Bun's default disposition killed the process with no chance to close out run.json. The new handler restores the terminal and finalizes run state itself rather than relying on OpenTUI's own listener to run first, since that would make correctness depend on a vendored listener's registration order and internals this codebase doesn't own; the terminal-restore call is idempotent so a redundant call from OpenTUI's own listener is harmless. A forked-pty regression test pins the empirical finding this design depends on: Bun's raw-mode stdin clears ISIG, so a real Ctrl+C keypress during an interactive session is delivered only as a stdin byte, never as a SIGINT, leaving the existing double-tap-to-quit gesture as the sole owner of in-session Ctrl+C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
runTUI's own try/catch (e.g. a fire-and-forgetvoidcall) reached the process-level uncaughtException/unhandledRejection handler, but that handler had no way to restore the terminal — it only knew aboutrun.jsonstate, not the terminal-restore closure bound insiderunTUI. A module-level slot (src/session/active-host.ts, mirroring the existingactive-run.tspattern) closes that gap.process.exit, so an external kill left the process (andrun.json) hanging. Outside an interactive session (exec mode, or before a host mounts), nothing handled the signal at all.disposeHostis idempotent, so a redundant call from OpenTUI's own listener is harmless.tests/integration/rawmode-sigint.test.ts) pins the empirical finding the whole design depends on: Bun's raw-mode stdin clears ISIG, so a real Ctrl+C keypress during an interactive session is delivered only as a stdin byte, never as SIGINT — the in-session double-tap-to-quit gesture stays the sole owner of in-session Ctrl+C.Test plan
bun run typecheckbun run buildbun run test(full suite via the project's serialized runner) — 4162 pass / 1 fail, the one failure (src/agent/lsp-availability.test.ts) is a pre-existing environment gap (missingtypescript-language-serverbinary in this worktree), unrelated to this changesrc/session/active-host.test.tstests/integration/signal-finalize.test.ts(SIGINT/SIGTERM/SIGHUP each finalizerun.jsonasfailedand exit with the correct128+ncode),tests/integration/rawmode-sigint.test.ts(pty-based, pins the raw-mode/ISIG finding)