GeezerVison is showing github.com — Worker-thread saturation under concurrent sessions: permission matching, turn-diff recomputation, event fanout, snapshot churn · Issue #49565 · anomalyco/opencode
Worker-thread saturation under concurrent sessions: permission matching, turn-diff recomputation, event fanout, snapshot churn #49565DescriptionSummary
With 8+ concurrent sessions (subagent lanes running bash-heavy work), a single instance visibly slows down: the Worker thread that processes session events saturates at ~90-99% of a core in userspace JS (no syscall blocking, no lock waits), and every lane queues behind it. Measured on a 16-core/62GB Linux box while 8 lanes ran: DB event rate ~2.4/s, WAL bounded by autocheckpoint, read/write lane split active, CPU/RAM headroom, network fine. The cost is per-event/per-step JS work on that one thread.
Contributors and fix directions
1. Permission matching path
packages/core/src/util/wildcard.ts compiles a fresh RegExp per comparison, called per rule per pattern for every tool call.
packages/opencode/src/permission/index.ts emits an INFO log line per pattern evaluation.
- Direction: cache compiled regexes (keyed pattern + platform flag), demote or throttle the per-pattern log line, hoist
rulesets.flat() out of the hot path.
2. Turn diff recomputation
packages/opencode/src/session/summary.ts runs on every step-finish and always diffs the first step-start tree against the last step-finish tree; the no-change dedup sits after the expensive diffFull, so step k re-diffs steps 1..k — quadratic jsdiff work per turn, multiplied by lane count.
- Direction: memoize the last computed
(from, to) pair per session/message, skip identical pairs, single-flight/coalesce per session, and short-circuit from === to before the git work.
3. Event fanout / SSE
packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts enqueues events for every connection into an unbounded queue and filters afterwards; RSS grows with lane count.
packages/opencode/src/event-v2-bridge.ts constructs a fresh Location.Info schema instance plus AbsolutePaths per published event even though they are constant per directory/workspace.
collapseEventBatch only merges adjacent deltas, which rarely happens while sessions interleave.
- Direction: filter before enqueue, bound queues, memoize the location payload, merge deltas by part key.
4. Snapshot churn
- Per-step
track() runs continuously under many lanes (~19/min observed) and all lanes share one derived snapshot gitdir, so their git transactions serialize; patch() re-runs the full add-scan immediately after the preceding track().
- Direction: reuse the staged index for
patch(), thread an unchanged flag into summarize, memoize exclude/sync scans. Do not debounce track() itself — the snapshot race tests depend on the current behavior.
5. Smaller items
- Doom-loop detection rebuilds a JSON needle per tool-result event.
- Unbounded in-memory maps (approved-permission list, TUI stores) grow with session count.
- Session-layer delta coalescing is worth doing only after the above items are measured; worker-thread sharding is out of scope.
Notes
- Suggested way to attribute the remaining cost precisely: run a controlled 8-session repro with
bun run src/index.ts --cpu-prof-md --cpu-prof-dir=/tmp/prof (the profiler cannot attach to a running process), plus an event-loop lag probe while the lanes run.
Activitygithub-actions commented on Sep 17, 2026
This issue doesn't fully meet our contributing guidelines.
What needs to be fixed:
- No issue template was used. Please use one of: Bug Report, Feature Request, or Question.
- The issue reads as AI-generated content (comprehensive multi-section analysis with precise file paths, measurements, and fix directions across 5 distinct subsystems). AI-generated walls of text are not acceptable per our guidelines.
- Bug reports should include reproducible steps and system information so maintainers can verify the problem independently.
Please edit this issue to address the above within 2 hours, or it will be automatically closed.
This issue may overlap with existing issues. Please check:
If you believe this was flagged incorrectly, please let a maintainer know. AceRothstein71 commented on Sep 17, 2026 Update: fixes for most of the contributors listed here have landed on PR #48638 (branch tip d84bbc6):
- Permission path: compiled wildcard regex cache, per-pattern log demoted, ruleset flattening and dedupe.
- Turn diff:
from === to short-circuit, per-session/message non-empty-only memo of the last turn diff, single-flight summarize, unchanged-step skip, and patch(hash, { to }) tree-to-tree diffs.
- Event fanout: SSE filter-before-enqueue with a bounded dropping queue and a
server.desync marker, memoized Location.Info payload, part-key delta merging, and delta skipping for plugin hooks.
Remaining known follow-ups: session-layer delta coalescing (measure-first) and bounding the TUI store maps. github-actions commented on Sep 17, 2026 This issue has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.
Feel free to open a new issue that follows our issue templates.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|