opencode icon indicating copy to clipboard operation
opencode copied to clipboard

tui: resume hangs at 100% CPU when a session has thousands of child sessions

Open pcwhy opened this issue 1 month ago • 5 comments

Summary

Resuming (opening) a session that has thousands of child subagent sessions makes the TUI spin at ~100% CPU indefinitely and never become interactive. With ~2,559 children it never recovers. With 558 or 778 children the same session opens normally. Deleting the child sessions fixes it immediately.

Environment

  • opencode version: 2.0.32 (latest channel at the time; 2.1.2 was already offered)
  • OS: macOS 15.8.1 (Darwin 24.6.0, x86_64)
  • Terminal: Apple Terminal, TERM=xterm-256color, TERM_PROGRAM=Apple_Terminal
  • Shell: /bin/zsh
  • Install/channel: release install (~/.opencode/bin/opencode), latest
  • Active plugins: none configured by the user (only built-in TUI plugins such as opencode.diffs, opencode.merman, opencode.latex)

Reproduction

  1. Have a session with a large transcript (here: 5,809 messages / 72 MB) and more than ~2,000 child sessions created through task subagents. A real-world session had 2,575 finished children.
  2. Resume it: opencode -s <session-id> (also reproduces with opencode -c, selecting it in the TUI, and opencode --standalone -s <session-id>).
  3. The TUI renders the first page, then pins one core at 100% CPU and stops responding to input.

Synthetic repro without needing a real subagent run:

  1. Export a session: opencode session export <id> > /tmp/parent.json.
  2. Import it once as a new parent, then import ~2,600 empty child sessions with POST /api/experimental/session/import, each with info.parentID pointing at the new parent (and a fresh info.id).
  3. opencode -s <new-parent-id>.

Reproducibility: 100% for us. It is not project-specific (reproduces in an empty directory) and not provider/model-specific.

Expected Behavior

The session opens to an interactive prompt, like any other session.

Actual Behavior

  • While loading, the client sends 2 HTTP requests per child session: GET /api/session/{childID}/form and GET /api/session/{childID}/permission (plus paginated child lists, 200 per page). For 2,559 children that is ~5,118 requests.
  • All of those requests complete in ~2 seconds (server responds in 3–40 ms each). After that the client spins forever at ~100% CPU.
  • sample <pid> of the hung process shows the main thread repeatedly in setTerminalTitle → writeOut (Bun), i.e. it never drains back to the event loop. No further HTTP traffic is sent.
  • The shared server stays healthy; GET /api/session/{id}, /message, /context, /diff all answer in milliseconds.
  • Memory grows to ~450–580 MB RSS; the screen keeps showing the loading spinner.

Additional Context

Measured threshold (same 5,809-message parent transcript, synthetic child sessions):

Child sessions Result
0 opens, CPU settles
558 opens (1,108 form/permission requests), CPU settles
778 opens, CPU settles
2,559 100% CPU hang
2,778 100% CPU hang

Other notes:

  • time_archived on the child sessions does not remove them from GET /api/session?parentID=..., so archiving is not a workaround.
  • Workaround that does work: delete the child sessions (DELETE /api/session/{id} / opencode session delete <id>), keeping the parent. After deleting 2,575 children, resume went from a permanent 100% CPU to ~3–7% and the full transcript was intact.
  • Data hygiene note: on this machine the DB had grown to 17 GB. A single parent's 558 children occupied 6.9 GB in session_message and 6.8 GB in the legacy part table. The child-session fan-out is the main source of DB bloat in our usage.
  • The hang is client-side: --standalone (private server) reproduces identically, and the shared server logs show no errors.

Relevant log lines during load (client):

message="cli starting" version=2.0.32 args="[\"-s\",\"<session-id>\"]" role=cli
message="event stream connected" component=client role=cli
# then ~5k requests:
GET /api/session/<child>/form -> 200
GET /api/session/<child>/permission -> 200
# then no further output; process stays at ~100% CPU

pcwhy avatar Oct 21 '26 04:10 pcwhy

Thanks for the detailed report and the synthetic repro.

I tried it on Linux with 2.0.32, using a virtual terminal: a parent imported with 6,000 messages, 2,600 empty child sessions imported with parentID pointing at it, then the parent opened through /sessions. Here the TUI spiked to about 110% CPU for a few seconds while loading, then settled and accepted input normally. I couldn't reproduce the permanent hang.

Your sample shows the main thread stuck in setTerminalTitle → writeOut, so this may depend on how the terminal handles output on macOS / Apple Terminal, which I can't test here. A maintainer with a Mac will need to look at it. Two things would help narrow it down:

  • Does it still hang in a different terminal on the same Mac, e.g. iTerm2, Ghostty or kitty?
  • Does it still hang if you turn off the terminal title setting in the TUI config, if your version has it?

opencode-agent[bot] avatar Oct 21 '26 04:10 opencode-agent[bot]

Thanks for looking — and an important correction after longer runs: it is not a permanent hang, it is a quadratic startup stall that looks like one.

On macOS x64, with the same synthetic parent (5,809-message transcript) and zero-message children (2.0.32 and 2.1.2 behave identically):

Children Time at ~100% CPU before the prompt appears
558 ~5 s
1,300 ~26 s
2,600 ~99 s

All HTTP traffic finishes in ~2 s; everything after that is pure client CPU with no terminal output. For the real 2,575-child session that means ~1.5 minutes of an unresponsive TUI, which is why users read it as a freeze. Your Linux run finishing in a few seconds is probably a faster/coalesced path, but the scaling is still quadratic (2x children → ~4x time).

Answers to your two questions (same Mac, 2,600 children):

  • iTerm2: still pinned at ~100% CPU for the full 20 s I sampled.
  • terminal.title=false (verified zero OSC title sequences are emitted): still pinned. So setTerminalTitle in the profile is just where the sampler caught it, not the cause.
  • Also still pinned with TERM_PROGRAM/TERM_SESSION_ID unset, TERM=dumb, a clean pty, and a 200x50 window. Not terminal-specific.

Likely cause in the 2.0.32 source:

  • packages/client/src/solid/data.ts:526-534 recursively lists all children (13 pages for 2,600).
  • registerSession (:559-578) pushes each child into store.session.family[root] one at a time via setStore.
  • packages/tui/src/routes/session/index.tsx:198-201 derives descendantSessionIDs from that array, and the effect at :321-328 re-runs on every change and calls permission.sync + form.sync for every descendant again.

That is O(n²): ~3.4M sync calls at 2,600 children, matching the ~99 s. Suggested fix: register fetched children in one batch (single setStore), and/or make the effect sync only newly added descendants instead of recomputing the whole set; or fetch child form/permission lazily when a child is actually opened.

Happy to test a build or provide the seeding script.

pcwhy avatar Oct 21 '26 05:10 pcwhy

A measured piece of this, from a session with 559 children (5,226 messages). On open, the session route syncs pending permissions and forms per descendant (1,118 requests), and each answer, even an empty one, writes a new key into the store, which re-runs every reader that walks the whole family (the session tabs' status, the program status, the tab rail): about 1,118 × 559 store reads. Making a sync that finds nothing pending, where nothing was, write nothing took launch-to-transcript from 31.7–35.8 s to 9.7–11.1 s (no plugins: 19.3 → 7.6 s): https://github.com/cevr/opencode/commit/a40157389789dc314bfa230797443f85a761c77e. PR #60925 (the family in one read) removes the per-member requests; my version of that was https://github.com/cevr/opencode/commit/cc890ed507664b44e05d351625f718f49898ea2d, so I'd take #60925. The empty-write fix still helps on top of it. Happy to send it as a PR.

cevr avatar Nov 11 '26 18:11 cevr

Independent confirmation of @cevr's finding, from a real session with 480 descendants (459 children, 21 grandchildren) on Linux.

I replayed that family in the TUI with the native renderer. After the 480 permission and 480 form reads return, opening the command palette takes 1579 ms, with a longest event-loop gap of 518 ms. With the empty results kept from each writing its own store key, the same open takes 93 ms (gap 98 ms). All 960 requests still run. The same root with no children takes 56 ms.

Caveats: this was a patched build based on dev @ 5418e010d7, and my patch batched the empty writes instead of skipping them. Skipping them, as in a401573, is simpler. I found no reader in packages/*/src that tells undefined from [] for these lists. The numbers cover the stall after the requests finish, not the whole open.

+1 for sending that commit as a PR.

WarGloom avatar Nov 13 '26 11:11 WarGloom

Windows 11 x64, Bun 1.4.4, upstream dev at 521ceac004. The extra reads still occur for completed children with no pending prompts in the production TUI with a loopback fixture. Each cold open returned the same 20 parent messages. With 0, 40, 200 and 220 synthetic children, each of three runs per size recorded exactly 0, 80, 400 and 440 child permission/form requests, respectively.

At 220 children the median input echo was 115 ms after selecting the parent, while the last child query reached the fixture at 460 ms. The traffic can outlast first input; those 460 ms were not time-to-interactive. This confirms the per-child work on Windows, not the multi-second stall at thousands of children. We used synthetic sessions, not production history.

521ox avatar Nov 18 '26 09:11 521ox