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: resolve memory leaks and zombie processes from missing cleanup handlers by dzianisv · Pull Request #13186 · anomalyco/opencode

Conversation

@dzianisv

@dzianisv dzianisv commented Feb 11, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Fixes #12687

Summary

Fix memory explosion (100GB+ RAM → macOS crash) caused by O(n²) allocation patterns in multiple hot paths and missing process cleanup handlers.

Changes

1. LLM response streaming (processor.ts) — Critical fix

  • Before: Every token delta triggered part.text += value.text (O(n²) string concat) + Session.updatePart() which does JSON.stringify(part) + Storage.write() + Bus.publish() — all per-token
  • After: Deltas accumulate in arrays with a throttled flush every 50ms. part.text is only built via chunks.join("") at flush time. updatePart() is called at most ~20x/sec instead of ~4000x for a typical response
  • Impact: For a 4K-token response (~20KB text), eliminates ~80MB of transient allocations per response

2. PTY terminal buffer (pty/index.ts)

  • Before: session.buffer += data — O(n²) string concatenation up to 2MB limit, then join+slice on overflow
  • After: chunks: string[] with length tracking, drop oldest chunks on overflow, send chunks individually on client connect

3. Bash tool output streaming (bash.ts)

  • Array accumulation + throttled metadata updates for command output

4. Shell command output streaming (prompt.ts)

  • Array accumulation + throttled preview updates for slash command output

5. Copilot SDK tool call arguments (openai-compatible-chat-language-model.ts)

  • Before: toolCall.function!.arguments += delta — O(n²) for large tool call arguments
  • After: argumentChunks.push() + .join("") pattern

6. Copilot message conversion (convert-to-openai-compatible-chat-messages.ts)

  • Before: text += part.text — O(n²) for multi-part assistant messages
  • After: textParts.push() + .join("") pattern

7. Ripgrep line reader (ripgrep.ts)

  • Before: buffer += decoder.decode(value) — O(n²) for large file listings
  • After: Concatenate only remainder + chunk per iteration (bounded to one line's worth)

8. WebFetch HTML text extraction (webfetch.ts)

  • Before: text += input.text in HTMLRewriter handler — O(n²) for large pages
  • After: chunks.push() + .join("") pattern

9. Process cleanup handlers

  • Signal handlers (SIGTERM/SIGINT/SIGHUP) added to index.ts to trigger Instance.disposeAll() on terminal close
  • Cleanup handlers for worker.ts, serve.ts, time.ts, next.ts, question/index.ts
  • Fixed serve.ts dead code: server.stop() was unreachable after await new Promise(() => {})

Verification

  • bun run typecheck — all packages pass
  • bun test — all tests pass (including new memory cleanup integration tests)
  • All 6 CI checks passing ✅

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 11, 2026

Copy link
Copy Markdown
Contributor

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

I found several related PRs that address similar issues with cleanup, signal handling, and preventing orphaned processes:

Potential Related/Duplicate PRs:

  1. fix: add signal handlers to prevent orphaned processes #12877 - fix: add signal handlers to prevent orphaned processes

    • fix: add signal handlers to prevent orphaned processes #12877
    • Directly addresses signal handlers for orphaned processes, which is a core issue in PR fix: resolve memory leaks and zombie processes from missing cleanup handlers #13186
  2. fix: prevent orphaned worker process when killed #11603 - fix: prevent orphaned worker process when killed

    • fix: prevent orphaned worker process when killed #11603
    • Addresses orphaned worker processes, related to the worker cleanup mentioned in the PR
  3. fix(serve): add signal handlers for graceful shutdown on Windows #9866 - fix(serve): add signal handlers for graceful shutdown on Windows

    • fix(serve): add signal handlers for graceful shutdown on Windows #9866
    • Related to signal handlers in serve.ts mentioned in the PR
  4. fix(mcp): Fix memory leaks in OAuth transport and process cleanup #9145 - fix(mcp): Fix memory leaks in OAuth transport and process cleanup

    • fix(mcp): Fix memory leaks in OAuth transport and process cleanup #9145
    • Addresses memory leaks and process cleanup, similar concerns to this PR

You should verify the status of PR #12877 in particular, as it appears to be a very recent and directly related fix for the same orphaned process issue.

gnadaban 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.

@dzianisv

dzianisv commented Feb 11, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Author

Self-Review: What the tests prove (and don't)

Tests that passed (10 new tests, all green)

Test What it proves
signal handler test: SIGTERM triggers graceful shutdown Registering process.on("SIGTERM", ...) prevents default kill and runs our handler. This is the exact pattern we added to index.ts.
Instance.disposeAll kills detached child processes Instance.dispose() runs registered State dispose callbacks (framework test, not a direct child-kill test)
bash tool ring buffer bounds memory 5MB output produces <30MB growth and metadata preview stays capped at 30KB
bash tool metadata preview does not grow with output After preview caps at 30KB, no further growth (proves O(1) per chunk)
PermissionNext: pending promises rejected on dispose Single pending permission promise is rejected with RejectedError on Instance.dispose()
PermissionNext: multiple pending promises all rejected 5 concurrent pending promises all rejected on dispose
Question: pending promises rejected on dispose Pending question promise rejected with RejectedError on dispose
FileTime: state cleared on dispose Session read tracking data gone after dispose, fresh state on re-provide
PermissionNext: dispose does not affect already-resolved Dispose with no pending promises is a safe no-op
Instance.disposeAll is idempotent Calling disposeAll() twice doesn't throw and state is properly cleared

What's NOT tested

  1. We didn't test Instance.disposeAll() actually kills LSP/MCP child processes. The LSP and MCP modules register their own dispose callbacks that kill child processes — those are existing code that we didn't modify. We tested that the dispose framework runs callbacks; we didn't test that those specific callbacks kill processes.

  2. The worker.ts and serve.ts signal handlers were not directly tested. The signal handler test validates the pattern works in general, but doesn't test the specific interaction between worker/serve and index.ts handlers racing.

Probability each fix helps with the reported issue (#12687)

Fix Probability Reasoning
Signal handlers (index.ts) High (80-90%) Without signal handlers, SIGHUP on terminal close kills the main process without running Instance.disposeAll(), orphaning all LSP/MCP/bash child processes. This is the #1 cause of orphaned processes.
Signal handlers (worker.ts) Low-Medium (30%) Workers share the process signal handlers. The main thread handler in index.ts already fires. Defensive backup, architecturally questionable but safe (disposeAll is idempotent).
serve.ts infinite promise fix Medium (60%) The await new Promise(() => {}) made server.stop() unreachable dead code. Now the serve command can actually shut down gracefully. The Instance.disposeAll() here is redundant with index.ts but harmless.
Bash ring buffer Medium (50%) The O(n²) string concat is algorithmically real and tested, but causes transient spikes, not persistent leaks. The user reported 9.4GB sustained usage — more consistent with orphaned processes than transient spikes.
Prompt.ts shallow copy Low (20%) Saves one deep clone per LLM round-trip. Meaningful for GC pressure but unlikely the cause of 9.4GB.
Permission/Question dispose Medium (40%) Real fixes for mid-life disposal (config changes, workspace reset). Prevents hanging sessions and promise leaks in the running process. But only triggers during specific user actions, not the common "close terminal" scenario.
FileTime dispose Cosmetic (5%) The framework already cleans up state references via entries.clear() in state.ts:64. Our callback just zeroes properties on an object that's about to become unreachable anyway. Tested and harmless, kept for defense-in-depth.

Test results

  • Full suite: 907 pass, 1 skip, 2 fail (pre-existing Bedrock/AWS network timeouts unrelated to these changes)
  • Typecheck: 0 errors
All reactions

Sorry, something went wrong.

Uh oh!

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

@dzianisv

dzianisv commented Feb 11, 2026

Copy link
Copy Markdown
Author

Response to related PRs check

I reviewed all 4 flagged PRs. None have been merged into dev as of today. Here is the overlap analysis:

PR #12877 — signal handlers to prevent orphaned processes

Direct overlap with our index.ts changes. Both add SIGTERM/SIGINT/SIGHUP handlers calling Instance.disposeAll(). Key differences:

  • fix: add signal handlers to prevent orphaned processes #12877 creates a separate signal.ts module with a 5-second force-exit timeout and Unix exit codes (128+signal). Also handles SIGQUIT.
  • Our PR inlines the handler in index.ts (simpler, follows existing code style) and also adds Instance.disposeAll() to the finally block so normal exit paths also clean up.

Assessment: #12877 has a cleaner modular design with the timeout safety net. Our approach is simpler but covers the finally block too. If #12877 is merged first, we should drop our index.ts signal handler changes and keep only the finally block addition.

PR #11603 — prevent orphaned worker process when killed

Partial overlap with our worker.ts changes. Both add signal handlers to the worker. Key differences:

  • fix: prevent orphaned worker process when killed #11603 also adds parent-death detection via process.ppid polling (1-second interval) and adds shutdown logic to thread.ts (main TUI thread).
  • Our PR only adds signal handlers to worker.ts.

Assessment: #11603 is more thorough — the parent-death polling catches the case where the main process is SIGKILL'd (which no signal handler can intercept). If #11603 is merged first, we should drop our worker.ts changes entirely.

PR #9866 — signal handlers for graceful shutdown on Windows

Partial overlap with our serve.ts changes. Both address graceful shutdown of the serve command. Key differences:

  • fix(serve): add signal handlers for graceful shutdown on Windows #9866 adds signal handlers but keeps the unreachable await new Promise(() => {}) + dead server.stop() code.
  • Our PR replaces the infinite promise with signal-based exit and adds Instance.disposeAll().

Assessment: Our serve.ts fix is more complete since it also removes the dead code. #9866 just adds handlers above the existing broken pattern. These could coexist but ours makes #9866's changes redundant.

PR #9145 — memory leaks in OAuth transport and process cleanup

Minimal overlap. #9145 focuses on MCP OAuth transport lifecycle (closeTransport, setPendingOAuthTransport) which we do not touch. Its index.ts changes overlap with ours (signal handlers + main() wrapper with cleanup) but the MCP-specific fixes are orthogonal.

Assessment: The MCP OAuth transport fixes in #9145 are valuable and complementary to our PR. If both merged, only the index.ts changes conflict.


Summary

Our PR has unique value beyond what the other PRs offer:

  1. Bash tool ring buffer (O(n²) → O(1) per chunk) — not in any other PR
  2. Prompt.ts shallow copy — not in any other PR
  3. PermissionNext/Question/FileTime dispose callbacks — not in any other PR
  4. serve.ts dead code removal — fix(serve): add signal handlers for graceful shutdown on Windows #9866 does not fix this
  5. finally block Instance.disposeAll() — fix: add signal handlers to prevent orphaned processes #12877 does not add this

The signal handler overlap (index.ts, worker.ts) is expected since multiple people independently identified the same root cause. Whichever PR is merged first, the others should rebase and drop the duplicate changes.

All reactions

Sorry, something went wrong.

Uh oh!

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

@dzianisv
dzianisv force-pushed the fix/memory-leak-cleanup branch from 390cdd9 to c7f7ddb Compare February 13, 2026 05:34
@github-actions github-actions Bot added the needs:issue label Feb 13, 2026
@github-actions

github-actions Bot commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

All reactions

Sorry, something went wrong.

Uh oh!

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

engineer added 5 commits February 12, 2026 21:57
engineer
fix: resolve memory leaks and zombie processes from missing cleanup h…
4650ec6
…andlers

- Add SIGTERM/SIGINT/SIGHUP signal handlers to index.ts, worker.ts, and
  serve.ts that call Instance.disposeAll() to clean up child processes
  (LSP servers, MCP servers, bash tool processes spawned with detached:true)
- Replace O(n²) string concatenation (output += chunk.toString()) in bash.ts
  and prompt.ts shell execution with Buffer[] ring buffer + capped preview
- Replace deep clone of entire message history in prompt.ts with shallow copy
  (only the parts that get mutated need copying)
- Add dispose callbacks to PermissionNext, Question, and FileTime state to
  reject pending promises and clear session data on instance disposal

Fixes anomalyco#12687
engineer
test: add integration tests for memory leak fixes
1aa4c0f
- Signal handler pattern test: verifies SIGTERM triggers graceful handler
  instead of default kill behavior (proves index.ts signal handler works)
- Bash ring buffer memory test: verifies output metadata preview stays
  capped at 30KB and total memory growth is bounded for large output
- Dispose callback tests: verifies PermissionNext and Question pending
  promises are rejected with RejectedError on Instance.dispose()
- FileTime state cleanup test: verifies session data is cleared on disposal
- Instance.disposeAll() idempotency test: verifies safe double-call
engineer
fix: eliminate O(n²) string concatenation and throttle metadata updates
3dc1981
Replace preview += chunk.toString() with string[] array accumulation
joined only on flush. Throttle Session.updatePart() / ctx.metadata()
calls to at most every 100ms via setTimeout.

In prompt.ts shell(), reduce preview cap from 1MB to 30KB
(MAX_METADATA_LENGTH) to match bash.ts.

These three changes together eliminate ~64GB of transient allocations
that caused 100GB RAM consumption and macOS kernel panics when running
verbose commands.
engineer
fix: eliminate per-token O(n²) allocations in LLM streaming and PTY b…
66ce697
…uffer

- processor.ts: accumulate text/reasoning deltas in arrays with throttled
  flush (50ms) instead of per-token part.text += and Session.updatePart()
  calls, eliminating O(n²) string concatenation and quadratic
  JSON.stringify/Storage.write/Bus.publish overhead
- pty/index.ts: replace buffer string concatenation with chunks array
  and length tracking, drop oldest chunks on overflow instead of
  join+slice, send chunks individually on client connect
engineer
fix: use readiness signal instead of sleep in SIGTERM test to avoid r…
bec8ecd
…ace condition
@dzianisv
dzianisv force-pushed the fix/memory-leak-cleanup branch from 7134572 to bec8ecd Compare February 13, 2026 06:02
engineer added 2 commits February 12, 2026 22:06
engineer
fix: avoid disposeAll in test to prevent cross-test unhandled rejections
8c49616
engineer
fix: eliminate O(n²) string concatenation in ripgrep, copilot streami…
8133563
…ng, and webfetch
@github-actions github-actions Bot removed the needs:issue label Feb 13, 2026
@dzianisv dzianisv mentioned this pull request Feb 13, 2026
[BUG/PERF] Severe Memory Leak and Disk Swell leading to System Kernel Panic (macOS) #12687
Closed
This was referenced Feb 13, 2026
fix: resolve multiple memory leaks causing unbounded RAM growth ( Now passing: && Fixed failing tests for windows ) #13514
Closed
fix(tui): add signal handlers to prevent orphaned processes on terminal close #13848
Closed
fix(opencode): run tui worker shutdown on signal/terminal-exit paths #14238
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: cleanup SDK event listeners to prevent memory leaks #14635
Closed
fix(web): dispose idle MCP server instances to prevent process accumulation #15326
Closed
This was referenced Mar 1, 2026
fix: kill orphaned MCP child processes and expose OPENCODE_PID on shu… #15516
Merged
fix(pty): pty session handle leak #15599
Merged
fix(opencode): robust process exit detection for child processes #15757
Closed
@binarydoubling binarydoubling mentioned this pull request Mar 9, 2026
Multiple memory leaks cause unbounded RAM growth during extended TUI usage #16697
Closed
@binarydoubling binarydoubling mentioned this pull request Mar 9, 2026
fix: resolve multiple memory leaks causing unbounded growth #16695
Closed
3 of 6 tasks
@github-actions github-actions Bot mentioned this pull request Mar 21, 2026
Opencode now respects SIGTERMs and shuts down gracefully (K8 prehook … #18555
Closed
6 tasks
@github-actions github-actions Bot mentioned this pull request Mar 21, 2026
Opencode now respects SIGTERMs and shuts down gracefully (K8 prehook … #18556
Closed
6 tasks
@OpenCodeEngineer

OpenCodeEngineer commented Mar 25, 2026

Copy link
Copy Markdown

Closing this upstream PR to keep active work on the fork only. Current workflow: open PRs on dzianisv/opencode, merge into fork main, and do not keep active PRs in anomalyco/opencode.

All reactions

Sorry, something went wrong.

Uh oh!

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

@dzianisv dzianisv closed this Mar 25, 2026
@Astro-Han Astro-Han mentioned this pull request May 6, 2026
[Bug] Desktop webfetch fails on HTML pages requested as text Astro-Han/pawwork#466
Closed
@Innerpeace1990 Innerpeace1990 mentioned this pull request May 7, 2026
Bug: webfetch with format="text" crashes on HTML content in Electron desktop app (HTMLRewriter is not defined) #26187
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

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.

[BUG/PERF] Severe Memory Leak and Disk Swell leading to System Kernel Panic (macOS)

Uh oh!

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

2 participants

@dzianisv @OpenCodeEngineer
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.