fix: multiple memory leaks in long-running sessions #7783

Open
opened 2026-02-16 18:08:13 -05:00 by yindo · 2 comments
Owner

Originally created by @sgInnora on GitHub (Jan 27, 2026).

Originally assigned to: @rekram1-node on GitHub.

Bug Description

Several memory leaks occur during long-running opencode sessions, causing RSS to grow continuously:

  1. AsyncQueue never terminates (util/queue.ts) - [Symbol.asyncIterator] loops forever with no close mechanism, preventing GC of consumers.

  2. Bash tool unbounded string concatenation (tool/bash.ts:167-189) - stdout/stderr appended via output += chunk.toString() with no size limit. Commands producing large output (e.g., find /) cause unbounded memory growth.

  3. LSP diagnostics Map never cleared (lsp/client.ts:51) - diagnostics Map accumulates entries indefinitely. shutdown() disposes the connection but doesn't clear the map.

  4. Bus subscriptions Map not cleared on dispose (bus/index.ts) - Instance dispose callback fires wildcard subscribers but never calls subscriptions.clear().

  5. PTY buffer string concatenation (pty/index.ts:136-150) - session.buffer += data then session.buffer.slice(-BUFFER_LIMIT) causes repeated large string allocation and GC pressure.

  6. Instance.disposeAll() not called on process exit (index.ts) - The main finally block calls process.exit() without disposing instances, leaking subprocess handles.

Reproduction

  1. Start opencode, run a command producing large output (find / -name "*.ts")
  2. Monitor with top -pid <PID> - RSS grows without bound
  3. Idle for 10+ minutes - memory does not decrease

Environment

  • opencode v1.1.36
  • macOS Darwin 24.6.0
  • Bun 1.3.5
Originally created by @sgInnora on GitHub (Jan 27, 2026). Originally assigned to: @rekram1-node on GitHub. ## Bug Description Several memory leaks occur during long-running opencode sessions, causing RSS to grow continuously: 1. **AsyncQueue never terminates** (`util/queue.ts`) - `[Symbol.asyncIterator]` loops forever with no close mechanism, preventing GC of consumers. 2. **Bash tool unbounded string concatenation** (`tool/bash.ts:167-189`) - stdout/stderr appended via `output += chunk.toString()` with no size limit. Commands producing large output (e.g., `find /`) cause unbounded memory growth. 3. **LSP diagnostics Map never cleared** (`lsp/client.ts:51`) - `diagnostics` Map accumulates entries indefinitely. `shutdown()` disposes the connection but doesn't clear the map. 4. **Bus subscriptions Map not cleared on dispose** (`bus/index.ts`) - Instance dispose callback fires wildcard subscribers but never calls `subscriptions.clear()`. 5. **PTY buffer string concatenation** (`pty/index.ts:136-150`) - `session.buffer += data` then `session.buffer.slice(-BUFFER_LIMIT)` causes repeated large string allocation and GC pressure. 6. **Instance.disposeAll() not called on process exit** (`index.ts`) - The main `finally` block calls `process.exit()` without disposing instances, leaking subprocess handles. ## Reproduction 1. Start opencode, run a command producing large output (`find / -name "*.ts"`) 2. Monitor with `top -pid <PID>` - RSS grows without bound 3. Idle for 10+ minutes - memory does not decrease ## Environment - opencode v1.1.36 - macOS Darwin 24.6.0 - Bun 1.3.5
yindo added the perf label 2026-02-16 18:08:13 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 27, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #9699: Memory usage grows very quickly when running commands with large output - directly addresses the bash tool unbounded string concatenation issue you identified
  • #9143: Memory leak: LSP client diagnostics and files maps not cleared on shutdown - addresses the LSP diagnostics Map memory leak issue
  • #9157: Memory leak: Instance disposal doesn't clean up plugins or cached resources - addresses the Instance.disposeAll() not being called issue
  • #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit - related to subprocess handle leaks and process exit cleanup
  • #9140: Memory leak: Unbounded caches grow without limit - addresses the general unbounded cache growth pattern

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 27, 2026): This issue might be a duplicate of existing issues. Please check: - #9699: Memory usage grows very quickly when running commands with large output - directly addresses the bash tool unbounded string concatenation issue you identified - #9143: Memory leak: LSP client diagnostics and files maps not cleared on shutdown - addresses the LSP diagnostics Map memory leak issue - #9157: Memory leak: Instance disposal doesn't clean up plugins or cached resources - addresses the Instance.disposeAll() not being called issue - #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit - related to subprocess handle leaks and process exit cleanup - #9140: Memory leak: Unbounded caches grow without limit - addresses the general unbounded cache growth pattern Feel free to ignore if none of these address your specific case.
Author
Owner

@KaranjotVendal commented on GitHub (Feb 2, 2026):

I believe I have encountered the same issue. Seems like the following PR #11717 and PR #10914 seems to be fixing the solution.

Image

@KaranjotVendal commented on GitHub (Feb 2, 2026): I believe I have encountered the same issue. Seems like the following PR #11717 and PR #10914 seems to be fixing the solution. ![Image](https://github.com/user-attachments/assets/93533551-0a85-4747-852e-51f6f742a844)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7783