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
commented
Feb 11, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
gnadaban reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dzianisv
commented
Feb 11, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
-
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.
-
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
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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:
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:
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:
- Bash tool ring buffer (O(n²) → O(1) per chunk) — not in any other PR
- Prompt.ts shallow copy — not in any other PR
- PermissionNext/Question/FileTime dispose callbacks — not in any other PR
serve.ts dead code removal — fix(serve): add signal handlers for graceful shutdown on Windows #9866 does not fix this
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for your contribution!
This PR doesn't have a linked issue. All PRs must reference an existing issue.
Please:
- Open an issue describing the bug/feature (if one doesn't exist)
- Add
Fixes #<number> or Closes #<number> to this PR description
See CONTRIBUTING.md for details.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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
- 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
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.
…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
This was referenced Feb 13, 2026
This was referenced Feb 20, 2026
This was referenced Mar 1, 2026
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|