Skip to content

fix(session): Memory keeps growing until the bun process is killed. #35107

Closed as not planned

Description

@xingruodong-sys

Description

updatePart calls structuredClone(part) on every part update. Text parts accumulate content as tokens stream in (up to 488 KB per part). With ~93K PartUpdated events over 200 sessions,, this creates massive heap pressure.

Root Cause

Bun's allocator (mimalloc) does not return freed pages to the OS, so RSS grows continuously — observed ~450 MB at startup → 8 GB after 20 h.

Proposed Fix

Remove structuredClone. The PartUpdated projector only destructures the part and writes to SQLite — it never mutates the original object. opencode/packages/opencode/src/session/session.ts:

    const updatePart = <T extends SessionV1.Part>(part: T): Effect.Effect<T> =>
      Effect.gen(function* () {
        yield* events.publish(SessionV1.Event.PartUpdated, {
          sessionID: part.sessionID,
          part,
          time: Date.now(),
        })
        return part
      }).pipe(Effect.withSpan("Session.updatePart"))
  • Process RSS before fix: 500 MB → 8 GB over 200 sessions,
  • Process RSS after fix: stable at ~500 MB
  • The PartUpdated projector (session/projectors.ts) only reads from part:
    const { id, messageID, sessionID, ...rest } = data.part
    db.insert(PartTable).values({ ... data: rest }).onConflictDoUpdate(...)

It works fine for several days.

Plugins

No response

OpenCode version

1.17.8

Steps to reproduce

  1. run opencode web
  2. Multiple users on the local area network are using it concurrently. After one day, there are about 200 sessions, and after multiple rounds of dialogue.
  3. check the memory status.

Screenshot and/or share link

No response

Operating System

Ubuntu24.04

Terminal

Bash

Activity

  1. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

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

    Your specific analysis of structuredClone in updatePart is a concrete and useful finding. If you haven't already, consider also uploading a heap snapshot following the instructions in #20695 to help the maintainers confirm and prioritize the fix.

  2. Hubxx520 commented on Jul 3, 2026

    @Hubxx520

    这个问题确实存在,请予以重视,不能未来堆功能而忽视内存损耗。

  3. 1884000 commented on Jul 3, 2026

    @1884000

    +1

  4. shashb27 commented on Aug 20, 2026

    @shashb27

    Triage: linked PRs do not address this issue

    Cross-checking the PRs linked to this issue, neither actually fixes the structuredClone(part) leak described here:

    PR #42150 — targets a different bug

    PR #35111 — the real fix, stale-closed

    • Author is the issue reporter (@xingruodong-sys). Branch fix/35107-memory-grows. Title matches this issue. Closed 2026-08-03 by the github-actions stale-PR bot ("Automated PR Cleanup: >1 month old, <2 positive reactions"), not by a maintainer.
    • It was not merged and not maintainer-rejected — it rotted due to inactivity.

    Net status

    No merged or open PR currently addresses #35107. The issue is effectively unowned right now.

    Suggested next step

    A maintainer should decide one of:

    1. Reopen PR fix:Memory keeps growing until the bun process is killed #35111 and review it (the one-line structuredClone(part) → part removal, guarded by confirming the projector in session/projectors.ts only destructures/inserts and never mutates part).
    2. Accept a fresh PR from another contributor (the fix is a one-line removal; the issue body already proposes it).

    I'm happy to open a clean PR if a maintainer confirms. Not opening one unilaterally to avoid duplicating #35111.

    (Context: I hit this while investigating memory bloat in my own opencode sessions — reported in capsule oxmiq/capsule#1762. The structuredClone per-PartUpdated path is the strongest match for "many subagents + tokens = RSS growth" among the open upstream issues.)

  5. 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