Skip to content

Memory leak in AI SDK Effect runtime — EventTarget listeners never cleaned up #34574

Closed as not planned

Description

@fifaliao

opencode version: 1.17.11 (bun-compiled binary)
OS: Ubuntu 22.04, Linux x86_64
Deployment: systemd service, MemoryMax=8G

Description

The opencode web server has a severe memory leak. RSS grows from ~127MB to ~4.9GB in ~20 minutes of active use. The process becomes unresponsive, health check latency increases from <5ms to >2s, and eventually it must be force-killed.

Root cause

The leak originates in the AI SDK Effect runtime bundled inside the opencode binary. Each LLM call creates a new set of event listeners on a shared EventTarget ([vY]) that are never removed after the call completes.

The MaxListenersExceededWarning fires 27+ times per session:

MaxListenersExceededWarning: Possible EventTarget memory leak detected.
    11 event listeners added to [vY]. MaxListeners is undefined.
    Use events.setMaxListeners() to increase limit

      at overflowWarning (node:events:185:19)
      at addListener (node:events:158:22)
      at ~effect/Effect/evaluate (/$bunfs/root/chunk-624361m2.js:25:4492)
      at runLoop (/$bunfs/root/chunk-624361m2.js:25:2045)
      at evaluate (/$bunfs/root/chunk-624361m2.js:25:1435)
      at runTasks (/$bunfs/root/chunk-624361m2.js:11:12793)
      at afterScheduled (/$bunfs/root/chunk-624361m2.js:11:12680)

Every LLM call goes through this runtime:

llm runtime selected llm.runtime=ai-sdk llm.provider=opencode llm.model=big-pickle

Memory growth (measured)

Time RSS Health check latency
Fresh start 127 MB ~5ms
~10 min (moderate use) 439 MB ~50ms
~20 min (active use) 4,900 MB 971ms→2.85s

Growth rate: ~20 MB/min under moderate load, faster under heavy use (subagent parallelism).

Plugin verification

All 4 installed plugins were audited — none use EventTarget or EventEmitter:

  • oc-plugin-karpathy-guidelines (0.2.1) — only injects system prompt text
  • opencode-codegraph (0.1.37) — only scenario routing + tool definitions
  • omf — model fallback config
  • rtk.ts — local plugin

The leak reproduces identically regardless of plugins.

Impact

  • Process becomes unresponsive after 20-40 min of active use
  • Cannot gracefully shutdown (stuck in stop-sigterm state)
  • Requires SIGKILL to recover
  • Memory limit (MemoryMax=8G) prevents system OOM but does not fix the leak
  • All users running opencode web for extended periods are affected

Suggested fix location

The bundled file chunk-624361m2.js (inside the binary at /$bunfs/root/chunk-624361m2.js) contains the ~effect/Effect/evaluate function that registers listeners on the [vY] EventTarget. Each call to this function should clean up its listeners after the evaluation completes.

Activity

  1. github-actions commented on Jun 30, 2026

    @github-actions
    Contributor

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

  2. shashb27 commented on Aug 20, 2026

    @shashb27

    Triage: linked PR does not address this issue

    Cross-checking the PR linked to this issue, PR #38939 does not fix the AI-SDK Effect EventTarget leak described here:

    PR #38939 — targets a different leak

    Net status

    No open or closed PR addresses #34574. The leak is in a bundled/vendored AI SDK artifact (chunk-624361m2.js), so a fix likely needs either:

    1. An upstream AI-SDK dependency bump (if the listener leak is fixed in a newer SDK version), or
    2. A wrapper in opencode that removes listeners after each LLM evaluation completes.

    Suggested next step

    A maintainer should confirm whether #34574 and #36677 are considered duplicates (same symptom, different root cause) or distinct. If distinct, #34574 still needs a dedicated fix.

    (Context: I hit this while investigating memory bloat in my own opencode sessions — reported in capsule oxmiq/capsule#1762. The AI-SDK EventTarget listener leak is one of the two strongest matches for "many LLM calls = RSS growth" among the open upstream issues, alongside #35107.)

  3. nicekit37 commented on Sep 16, 2026

    @nicekit37

    Confirming this also affects normal interactive TUI sessions on Windows — not just opencode web on Linux.

    • opencode version: 1.18.31 (bun-compiled binary, installed via npm)
    • OS: Windows 11 x64, Windows Terminal
    • Context: TUI session with heavy subagent parallelism (multiple subagents spawned concurrently, subagent_depth: 2)

    The warning renders as a large multi-screen error block inside the TUI conversation, which is very noisy even though the session keeps working.

    Same Effect runtime frames as reported here (~effect/Utils/internal → ~effect/Effect/evaluate → runLoop), reached via getPrompt → request → send. The dumped emitter state before the trace differs slightly from the [vY] EventTarget in the original report — it shows a stream emitter with type: "drain", count: 11, so in the TUI path the listeners appear to accumulate on a Writable stream's drain event:

        at overflowWarning (node:events:185:19)
        at addListener (node:events:158:22)
        at once (node:events:200:36)
        at <anonymous> (B:/~BUN/root/chunk-k9zx24wq.js:69:2280)
        at new Promise (1:11)
        at send (B:/~BUN/root/chunk-k9zx24wq.js:69:2131)
        at <anonymous> (B:/~BUN/root/chunk-k9zx24wq.js:67:14427)
        at new Promise (1:11)
        at request (B:/~BUN/root/chunk-k9zx24wq.js:67:12703)
        at getPrompt (B:/~BUN/root/chunk-k9zx24wq.js:67:29206)
        at ~effect/Utils/internal (B:/~BUN/root/chunk-nhtrqkcr.js:8:1579)
        at <anonymous> (B:/~BUN/root/chunk-nhtrqkcr.js:25:5339)
        at ~effect/Effect/evaluate (B:/~BUN/root/chunk-nhtrqkcr.js:25:5625)
        at runLoop (B:/~BUN/root/chunk-nhtrqkcr.js:25:2045)
        at evaluate (B:/~BUN/root/chunk-nhtrqkcr.js:25:1435)
        ...
        at ~effect/Effect/successCont (B:/~BUN/root/chunk-nhtrqkcr.js:25:7810)
        at runLoop (B:/~BUN/root/chunk-nhtrqkcr.js:25:2045)
        at evaluate (B:/~BUN/root/chunk-nhtrqkcr.js:25:1435)
    

    (The B:/~BUN/root/... paths are the same /$bunfs/root/... virtual filesystem as in the original report — Windows renders the bundled drive as B:.)

    Still reproduces on 1.18.31, which is the current latest release.

  4. thdxr commented on Oct 11, 2026

    @thdxr
    Member

    Thanks for the report! OpenCode 2.0 is now out and development is focused on V2, so we're closing issues filed against OpenCode 1. Please upgrade (opencode upgrade --major, details: https://anoma.ly/notes/opencode-2-0), and if this is still relevant on 2.0, reopen this issue or open a new one with your 2.0 version.

    — from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

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