Skip to content

fix: cap long-lived queue and shell buffers#20642

Closed
jwcrystal wants to merge 2 commits into
anomalyco:devfrom
jwcrystal:fix/memory-queue-buffers
Closed

fix: cap long-lived queue and shell buffers#20642
jwcrystal wants to merge 2 commits into
anomalyco:devfrom
jwcrystal:fix/memory-queue-buffers

Conversation

@jwcrystal
Copy link
Copy Markdown

@jwcrystal jwcrystal commented Apr 2, 2026

Issue for this PR

Closes #20626

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This fixes two long-running memory growth paths that can make a single OpenCode window consume very large amounts of memory over time.

First, it bounds the SSE queues used by the event routes and adds explicit close behavior so stalled or disconnected consumers do not keep accumulating buffered events in memory. It also preserves the final InstanceDisposed event before shutdown instead of dropping it.

Second, it stops bash and prompt subprocess output from growing unbounded in memory. Large bash output is now spilled to a truncation file while keeping the existing truncated / outputPath behavior, and prompt subprocess output is capped in memory as well. I also added a small follow-up guard so AsyncQueue.close() fails fast if called without a sentinel instead of leaving the queue in a half-closed state.

How did you verify your code works?

I tested the queue and bash-output regressions locally:

  • bun test test/util/queue.test.ts
  • bun test test/tool/bash.test.ts --filter "large-output spill-to-file|truncation"
  • bun typecheck 2>&1 | grep -E "test/util/queue.test.ts|test/tool/bash.test.ts|src/util/queue.ts|src/server/routes/event.ts|src/server/routes/global.ts|src/tool/bash.ts|src/session/prompt.ts"

During push, the repo hook also ran successfully:

  • bun turbo typecheck

I did not mark the full package test suite here because it is currently blocked by an existing @ai-sdk/provider-utils import error unrelated to this patch.

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Prevent stalled SSE streams and large shell output from growing memory without bound while preserving final disposal events and recoverable full bash output via truncation files. Add regression tests for AsyncQueue close semantics and bash spill-to-file behavior.
Prevent AsyncQueue from entering a half-closed state when close() is called without a configured sentinel by failing fast instead. Add a regression test to lock the API behavior in place.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 2, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@rekram1-node
Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does OpenCode suffer from memory leaks? A single window is consuming over 20 GB of memory.

2 participants