Skip to content

prevent SSE event streams from unbounded memory growth #31087

Closed as not planned
Closed as not planned

Description

@ShamirSecret
## Description

Long-running sessions can make the opencode server worker grow memory until it becomes unresponsive.

I reproduced this while resuming an existing session and sending `continue`. A fresh session did not show the same behavior. During the bad run, the worker process reached about 9.5GB RSS:

```text
bun run --cwd packages/opencode --conditions=browser src/index.ts .

The likely cause is the SSE event stream buffering events without a bound. On upstream/dev, packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts uses:

Queue.unbounded<EventV2.Payload>()

and packages/opencode/src/server/routes/instance/httpapi/handlers/global.ts uses Stream.callback(...) for global events.

If the client is slow, disconnected, or cannot consume events fast enough during a long task, these event streams can retain too many queued events. The instance event stream also queues events before filtering them by instance/workspace.

Expected behavior: slow SSE consumers should not be able to grow server memory without limit. Older progress events can be dropped under backpressure as long as task execution and final state are unaffected.

Plugins

None relevant.

OpenCode version

upstream/dev as of commit 1399323b78a04229d9bfe00c7436d7f41770fda8.

Steps to reproduce

  1. Start opencode from the repo root:
bun dev .
  1. Open or resume a long-running session that emits many task/tool/progress events.
  2. Send continue repeatedly or let the session run for a long time.
  3. Watch the worker process RSS.

Expected: memory should plateau or fluctuate within a bounded range.

Actual: memory can keep growing until the worker becomes unresponsive.

Screenshot and/or share link

N/A

Operating System

Windows 11

Terminal

Windows Terminal / PowerShell

Activity

  1. github-actions commented on Jun 6, 2026

    @github-actions
    Contributor

    This issue doesn't fully meet our contributing guidelines.

    What needs to be fixed:

    • No issue template was used. Please use one of the three templates: Bug Report, Feature Request, or Question.
    • Required bug report fields are missing: OpenCode version, Operating System, Terminal, and Steps to Reproduce are all empty. These are needed to reproduce and validate the reported behavior.
    • The issue reads more like a PR description (with Root Cause, Proposed Fix, and Validation sections) than a bug report. If this is a bug, please describe what you observed and how to reproduce it. If you have a fix, consider opening a pull request.
    • The heavily structured, AI-generated style of writing is not acceptable per our contributing guidelines.

    Please edit this issue to address the above within 2 hours, or it will be automatically closed.


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

    If you believe this was flagged incorrectly, please let a maintainer know.

  2. added
    needs:complianceThis means the issue will auto-close after 2 hours.
    and removed
    needs:complianceThis means the issue will auto-close after 2 hours.
    on Jun 6, 2026
  3. github-actions commented on Jun 6, 2026

    @github-actions
    Contributor

    Thank you for updating the issue! It now includes all the required information — a clear description, steps to reproduce, version details, and system info. Everything looks good.

  4. github-actions commented on Aug 6, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

  5. qvkap commented on Sep 12, 2026

    @qvkap

    For anyone running into severe SSE event stream memory ballooning as described here:

    I created an experimental personal fork with workaround bounds: https://github.com/qvkap/opencode-clean

    Disclaimer:

    • This contains AI-assisted heuristic patches ("AI slop").
    • I am aware that project maintainers have requested contributors not to submit LLM-generated code or bugfixes upstream; this is not submitted as an official PR or recommendation.
    • This is NOT a complete or definitive solution. It only mitigates roughly ~20% of the memory exhaustion surface by putting defensive bounds on queues and diagnostic caches.
    • Deep runtime leaks (such as EventTarget listener accumulation in the bundled AI-SDK Effect runtime and Bun/V8 heap retention under heavy tool usage) remain unaddressed.

    Mitigation applied for this issue:
    In packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts, Queue.unbounded<EventV2.Payload>() was replaced with Queue.sliding<EventV2.Payload>(1_000) to prevent the worker process from accumulating gigabytes of unconsumed events when downstream consumers lag. Additional LRU caps (MAX_DIAGNOSTICS_FILES = 5_000, MAX_OPEN_FILES = 1_000) were placed on the LSP client state.

    Under moderate single-session use this prevents the worker from spiking to 9+ GB as quickly, though long-running heavy sessions will still eventually require restarting until a proper runtime-level fix lands upstream.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions