Skip to content

Commit 76ee4ab

Browse files
committed
fix: add SIGHUP/SIGTERM/SIGPIPE handlers to prevent orphaned processes
When a controlling terminal is destroyed (SSH disconnect, tmux pane close), opencode processes become orphaned because the TUI event loop blocks on stdin that will never produce input, preventing process.exit() from executing. Each orphaned instance leaks ~4.7 GB of RAM. On systems using tmux-based session managers (e.g. oh-my-opencode), this can accumulate to 60+ GB. Add signal handlers for SIGHUP, SIGTERM, and SIGPIPE that call process.exit(1) immediately, ensuring clean shutdown on terminal death. Fixes anomalyco#14504
1 parent f07e877 commit 76ee4ab

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎packages/opencode/src/index.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ process.on("uncaughtException", (e) => {
4545
})
4646
})
4747

48+
// Handle terminal death signals to prevent orphaned processes.
49+
// When a controlling terminal is destroyed (SSH disconnect, tmux pane close, etc.),
50+
// the kernel sends SIGHUP. Without this handler, the process continues running
51+
// indefinitely because the TUI event loop blocks on stdin that will never arrive,
52+
// preventing the `process.exit()` in the finally block from ever executing.
53+
// This was observed to leak ~4.7 GB per orphaned instance.
54+
for (const sig of ["SIGHUP", "SIGTERM", "SIGPIPE"] as const) {
55+
process.on(sig, () => {
56+
process.exit(1)
57+
})
58+
}
59+
4860
const cli = yargs(hideBin(process.argv))
4961
.parserConfiguration({ "populate--": true })
5062
.scriptName("opencode")

0 commit comments

Comments
 (0)