GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cli): handle SIGHUP to prevent orphaned processes on terminal close by shjeong92 · Pull Request #12718 · anomalyco/opencode

Conversation

@shjeong92

@shjeong92 shjeong92 commented Feb 8, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #10563, relates to #11225, #11527

When a terminal tab/window is closed, the OS sends SIGHUP to the process group. OpenCode has no SIGHUP handler, so the process ignores it and stays alive as an orphan under launchd (PPID=1), consuming memory indefinitely.

This adds SIGHUP and SIGTERM handlers to all long-running entry points:

  • index.ts: Safety net with 3s timeout fallback (covers all commands)
  • thread.ts: Graceful worker shutdown before exit (TUI mode)
  • worker.ts: Shutdown + ppid polling to detect parent death (safety net for SIGKILL)
  • attach.ts: Exit on signal (opencode attach mode)
  • serve.ts: Exit on signal (opencode serve mode)

The layered approach works like this: command-specific handlers do graceful cleanup and exit immediately, while the index.ts safety net force-exits after 3s if a command-specific handler hangs.

How did you verify your code works?

  • All 897 existing tests pass, 0 regressions
  • tsc --noEmit passes with no type errors
  • Added signal handling tests that spawn actual opencode serve, send SIGHUP/SIGTERM, and verify graceful exit (exit code 0 vs 129 without fix)
  • Manual test: start opencode, note PID, send kill -HUP <pid>, confirm process exits

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, here are the related PRs that might be duplicates or closely related:

Potentially Related PRs

  1. fix: prevent orphaned worker process when killed #11603 - fix: prevent orphaned worker process when killed

    • fix: prevent orphaned worker process when killed #11603
    • Directly addresses orphaned worker processes, similar issue scope
  2. fix: force kill MCP server processes on dispose to prevent orphan processes #7424 - fix: force kill MCP server processes on dispose to prevent orphan processes

    • fix: force kill MCP server processes on dispose to prevent orphan processes #7424
    • Handles orphaned process prevention through proper cleanup
  3. fix(pty): use proper process tree cleanup on disposal #5876 - fix(pty): use proper process tree cleanup on disposal

    • fix(pty): use proper process tree cleanup on disposal #5876
    • Related to process cleanup and signal handling

Note: PR #12718 (the current PR) appears to be a comprehensive solution addressing the same orphaned process issue that previous PRs have attempted to fix. Check if #11603 and #7424 are closed/merged and whether this PR supersedes them or if there's overlapping work.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shjeong92
shjeong92 force-pushed the fix/handle-sighup-prevent-orphaned-processes branch from 4591da7 to ecdb049 Compare February 8, 2026 16:27
@luisrudge

luisrudge commented Feb 8, 2026

Copy link
Copy Markdown

yeah I was just hit with this issue as well.

You have 68 opencode processes running, most dating back to Jan 28 - Feb 1. This happens when you close terminal windows without explicitly exiting opencode (Ctrl+C or /exit) - the processes keep running in the background.
These are consuming:

  • ~10-15% total CPU (even while idle)
  • ~8GB+ RAM combined
    To clean them up, you can kill all except the current session:

Kill all opencode processes except the current one (PID 76275)

ps aux | grep opencode | grep -v grep | grep -v 76275 | awk '{print $2}' | xargs kill

shjeong92 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Feb 9, 2026
fix(opencode): exit process on SIGHUP (#12767) #12865
Closed
fix: add signal handlers to prevent orphaned processes #12877
Closed
@goniz

goniz commented Feb 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Hmm I think that calling process.exit() explicitly skips some shutdown procedures which probably can cause corrupted writes (stdout/files etc)

Additionally it skips finally() blocks and other background work since it stops the event loop and exits synchronously.

Here is the Node docs which transfer to Bun as well: https://nodejs.org/api/process.html#processexitcode

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shjeong92

shjeong92 commented Feb 15, 2026

Copy link
Copy Markdown
Author

@goniz Thanks for the feedback! You're right — I've updated the signal handlers to avoid calling process.exit() directly. They now remove their own listener and re-raise the signal so the default OS handler runs, allowing finally blocks and pending I/O to complete normally.

The only place process.exit() remains is in the index.ts safety net, which fires after a 3s timeout as a last resort if graceful shutdown hangs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shjeong92
shjeong92 force-pushed the fix/handle-sighup-prevent-orphaned-processes branch 2 times, most recently from df894ca to f07b5c9 Compare February 15, 2026 00:50
goniz
goniz reviewed Feb 15, 2026
View reviewed changes
Comment thread packages/opencode/src/cli/cmd/serve.ts
Comment on lines +11 to +17
for (const signal of ["SIGHUP", "SIGTERM"] as const) {
const handler = () => {
process.off(signal, handler)
process.kill(process.pid, signal)
}
process.on(signal, handler)
}

@goniz goniz Feb 15, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if you can use the process.once() instead of doing this dance.
If not, extract to a utility function and document the pattern

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shjeong92 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@shjeong92 shjeong92 Feb 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good cal. switched all signal handlers to process.once() across the codebase (serve.ts, attach.ts, thread.ts, worker.ts, index.ts). No more manual off dance.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/opencode/src/cli/cmd/tui/worker.ts Outdated
if (ppid > 1) {
const monitor = setInterval(() => {
try {
process.kill(ppid, 0)

@goniz goniz Feb 15, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this can lie due to race conditions. What if the parent dies and his pid gets recycled?

You can try checking if the processed moved to live under init as ppid / p group? Check me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shjeong92 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@shjeong92 shjeong92 Feb 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. the stale PID + kill(pid, 0) approach is vulnerable to PID recycling. Replaced it with a live process.ppid check each interval: when the worker gets reparented to init (ppid === 1), we know the parent is gone. Also gated behind process.platform !== "win32" since ppid semantics don't apply there.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot mentioned this pull request Feb 16, 2026
fix(tui): add signal handlers to prevent orphaned processes on terminal close #13848
Closed
@shjeong92
shjeong92 force-pushed the fix/handle-sighup-prevent-orphaned-processes branch 2 times, most recently from 7b5e903 to ebeb3df Compare February 17, 2026 07:37
@github-actions github-actions Bot mentioned this pull request Feb 19, 2026
fix(opencode): run tui worker shutdown on signal/terminal-exit paths #14238
Closed
@s2bomb s2bomb mentioned this pull request Feb 19, 2026
TUI: signal exits leak child processes #14237
Closed
This was referenced Feb 20, 2026
fix: add SIGHUP/SIGTERM/SIGPIPE handlers to prevent orphaned processes #14505
Closed
fix: handle SIGHUP/SIGTERM/SIGPIPE to prevent orphaned processes #14548
Closed
fix(web): dispose idle MCP server instances to prevent process accumulation #15326
Closed
@github-actions github-actions Bot mentioned this pull request Mar 1, 2026
fix(pty): pty session handle leak #15599
Merged
3 of 6 tasks
@shjeong92
shjeong92 force-pushed the fix/handle-sighup-prevent-orphaned-processes branch from ebeb3df to c8d3c2e Compare March 3, 2026 01:33
@shjeong92
fix(cli): handle SIGHUP to prevent orphaned processes on terminal clo…
eaf161c
…se (anomalyco#10563)
@shjeong92
shjeong92 force-pushed the fix/handle-sighup-prevent-orphaned-processes branch from c8d3c2e to eaf161c Compare March 3, 2026 01:38
@shjeong92
test(e2e): make session question seeding more resilient
89c11de
@shjeong92
shjeong92 requested a review from adamdotdevin as a code owner March 3, 2026 02:03
vbuccigrossi pushed a commit to vbuccigrossi/opencode that referenced this pull request Mar 7, 2026
EBrown @claude
feat: port remaining Tier 1+2 upstream fixes and features
63e7373
Tier 1 bug fixes:
- Fix O(n²) bash output concatenation with StreamingOutput class (anomalyco#9693)
- Fix memory leaks in Bus.once, Format, Plugin, ShareNext, Bootstrap (anomalyco#13514)
- Fix FileTime race condition using actual file mtime instead of JS clock
- Free memory on compaction prune: clear output/attachments/metadata (anomalyco#7049)
- Throttle reasoning-delta storage writes to 50ms intervals (anomalyco#11328)
- Handle SIGHUP/SIGTERM to prevent orphaned processes (anomalyco#12718)
- Add process.once("close") handler for bash tool reliability

Tier 2 features:
- Support 1M context window for Anthropic models via beta header (anomalyco#14375)
- Input-only token counting for compaction with limit.input models
- MCP lazy loading: on-demand tool discovery via mcp_search tool (anomalyco#8771)
- MCP servers listed in system prompt when lazy mode enabled
- StreamingOutput: output_filter regex for build diagnostics
- LSP server cleanup callback for temp directory removal
- Extract formatSize utility from uninstall to shared util/format
- GitHub CI: fix Bus subscription leak in session event handler

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This was referenced Mar 21, 2026
Opencode now respects SIGTERMs and shuts down gracefully (K8 prehook … #18555
Closed
Opencode now respects SIGTERMs and shuts down gracefully (K8 prehook … #18556
Closed
This was referenced Apr 3, 2026
fix: use exit event instead of close to resolve bash tool hang with background processes #20901
Closed
fix(sdk): ensure opencode server closes cleanly via process groups #21626
Closed
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 2f2ed11 to 9f201d6 Compare April 16, 2026 21:55
This was referenced Apr 25, 2026
fix(opencode): resolve heap unlimited + orphan processes on Linux #24297
Closed
fix(tui): handle SIGINT and SIGTERM for graceful shutdown #24659
Closed
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adamdotdevin adamdotdevin Awaiting requested review from adamdotdevin
1 more reviewer
@goniz goniz goniz left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Memory Leak: Orphaned processes when terminal closes without explicit exit

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@shjeong92 @luisrudge @goniz
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.