Skip to content

Get sync git and gh off the main-process event loop (fixes #124 beachballs) - #229

Merged
currentbits merged 12 commits into
mainfrom
coder/app-beachballs-when-multiple-threads-run-4e7132
Aug 17, 2026
Merged

currentbits merged 12 commits into
mainfrom
coder/app-beachballs-when-multiple-threads-run-4e7132

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Fixes #124. With several agent threads streaming at once, the whole app intermittently froze with a macOS beachball. Every synchronous execFileSync call in the Electron main process blocks all streaming and all windows for its full duration, so N concurrent runs plus IPC traffic queued behind whichever one was blocking.

Measured, before and after

electron/beachball.js (added here, npm run beachball) runs the issue's own repro against the real stack — real Electron main process, real Store/createRunner/registerIpc, real preload bridge in a real BrowserWindow — with N concurrent CODER_SIMULATE=1 runs (free, no network) while the renderer hammers git:diff, git:status, files:list and git:syncInfo over real IPC. The main process measures its own event-loop lag with the same perf_hooks histogram the new CODER_LOOP_LAG probe uses, against a 10ms heartbeat. Missed heartbeats are the beachball: while the loop is blocked nothing paints and no stream is serviced.

Run on a worktree at this branch's merge-base (none of this work in it) and on this branch. Same machine, same workload, ~596 git IPC calls each.

4 concurrent runs, 15s

lag p50 lag p99 lag max heartbeats starved
before 1.3ms 63.4ms 149.3ms 595/1503 60.4%
after 1.3ms 2.0ms 33.6ms 1451/1504 3.5%

8 concurrent runs, 15s: 60.7% → 5.0% starved, p99 63.1ms → 2.0ms.

Before the fix the main process missed three of every five heartbeats for fifteen seconds straight. It also no longer degrades with thread count — sync lag scaled 18.7 → 31.3 → 53.7ms p99 at 2/4/8 threads, async stays flat at 1.1–3.8ms.

What changed

Async git (ssh.js, services.js, worktrees.js). #88 bounded sync git at 15s but explicitly deferred the async conversion. Added execCommandAsync alongside the existing sync execCommand, and converted gitStatus / gitSyncInfo / gitRepoInfo / gitFetch / gitPull / addProject / createProject, plus the worktrees.js hot reads: the diff HEAD path behind git:diff, and listFiles/lsFiles behind the @-mention autocomplete — which fired a synchronous git ls-files on every keystroke.

Async gh — the biggest offender, and not in the original issue's list. worktrees.js had a sync ghTry calling execFileSync(ghBin(), ...) with GH_TIMEOUT_MS = 30_000. gh is a network call, so one slow GitHub request froze every window and every streaming thread for up to 30 seconds — three orders of magnitude worse than the local git this issue was opened about. It was reachable from six IPC handlers, including the issues.js planboard ones that agents hit while other threads stream. Everything now routes through the pre-existing ghTryAsync, and sync ghTry is deleted rather than left as a landmine; worktrees.js no longer imports execFileSync at all.

Store flush backoff + lag probe (store.js, looplag.js). Measured first rather than assumed: JSON.stringify of the live store is ~58ms, because coder-store.json on a real install is 54MB — 97% of it messagesByThread across 227 threads. #89's per-thread caps work correctly; the store is simply linear in thread count, which no per-thread cap can bound. At a 250ms debounce under concurrent streams that spent a quarter of the event loop serializing. The debounce now backs off 250ms → 2s while dirty and resets when quiet. CODER_LOOP_LAG=1 enables perf_hooks.monitorEventLoopDelay() and logs mean/p50/p99/max every 5s, off by default with no histogram created when unset.

which memoisation (providers.js). runner.js called defaultWhich on every run start, uncached, so N concurrent runs paid N PATH walks. Hits are cached; misses deliberately are not, so installing a provider CLI while the app is open still works without a restart. Memoised rather than made async — a cache hit beats an async spawn and avoids the caller cascade.

A latent bug fixed and pinned (ipc.js). git:syncInfo, git:repoInfo and git:pull document "never throws" and answer in-band. They were written as return services.gitFoo(root) inside a try/catch; once those functions became async the returned promise settles outside the try, so the catch could no longer see a rejection. Nothing fails today only because those functions catch their own errors — which is exactly why it needed pinning before a future throw path makes it live. Fixed with return await, plus electron/test/ipc-async-guard.test.js, verified to fail 3/3 with the awaits removed.

Tradeoffs and deliberate remainders

Testing

  • electron 859/860, renderer 723/723, core 38/38, tsc --noEmit clean.

  • The one electron failure is wireClient × webBridge over a real socket, a pre-existing port-allocation flake under parallel runs that passes 18/18 when its file runs alone. Present on main before this work.

  • New tests: ipc-async-guard.test.js, which-cache.test.js, gh-async.test.js, looplag.test.js, plus additions to the store, services and worktrees suites. The two I wrote myself I verified fail without their fix.

  • Note for anyone testing in a worktree: clone deps with cp -Rc from the main checkout rather than npm install, and run cd core && npm run build first — npm run test:electron needs a built core/dist or ~140 tests fail on a missing module.

  • PR created by the Claude Code agent

Willem and others added 11 commits August 17, 2026 11:30
JSON.stringify of the live store is ~58ms (53MB, 226 threads), so a
250ms debounce under concurrent streams spends a quarter of the event
loop just serializing. Double the delay on each dirty flush up to 2s
and reset when quiet. saveNow stays sync for process.on('exit').

CODER_LOOP_LAG=1 turns on perf_hooks.monitorEventLoopDelay and logs
mean/p50/p99/max every 5s. Off by default, no histogram created.
Concurrent agent threads beachballed because git status/fetch/pull and
addProject ran execFileSync on the Electron main process. Add
execCommandAsync next to the existing sync execCommand (worktrees.js
still needs the sync export) and await it from the services git helpers.
…into coder/app-beachballs-when-multiple-threads-run-4e7132
…ball

diff() and listFiles() were still calling execFileSync on the main
process (bounded at 15s by #88). With several agent runs streaming at
once those reads queued every other thread behind them.

Convert only the hot read paths to execFile. Write actions (merge, push,
PR, setup) stay sync. gitTryAsync now carries the sync error field and
treats async SIGTERM kills as timeouts.
)

git:syncInfo, git:repoInfo and git:pull document "never throws" and answer
in-band. Once services.gitSyncInfo & co. became async, `return fn(root)`
inside their try/catch settled the promise outside the try, so the catch
could no longer see a rejection. Nothing fails today because those
functions catch their own errors, which is exactly why this needed pinning
before a future throw path makes it a live bug.

Co-Authored-By: Claude Opus 5 <[email protected]>
… Stopping a run leaves agent grandchildren alive (no process-
…124)

defaultWhich is execFileSync on the main-process event loop and runner.js
calls it on every run start, uncached, so concurrent runs each paid a PATH
walk. Cache hits only: a miss stays uncached so installing a provider CLI
while the app is open still works without a restart.

Memoised rather than made async — a cached lookup is both faster than an
async spawn and a far smaller diff than the caller cascade.

Co-Authored-By: Claude Opus 5 <[email protected]>
Planboard and PR status/list/checks were still calling execFileSync gh
with a 30s timeout on the main process. Route those through the existing
ghTryAsync helper so a hanging GitHub call cannot beachball every window.
…into coder/app-beachballs-when-multiple-threads-run-4e7132

# Conflicts:
#	electron/main.js
W4 converted the hot read paths but left createPr and mergePr on sync gh
because write paths were scoped out. That left a 30s network call able to
freeze every window and every streaming thread on a single click, which is
the reported bug — so finish it.

mergePr was already async (3 awaits). createPr becomes async; its local git
(push/defaultBranch/gitTry) stays sync, still bounded at 15s by #88 — the
30s NETWORK calls were the point. With no callers left, sync ghTry is
deleted rather than left as a landmine, and worktrees.js no longer imports
execFileSync at all.

Co-Authored-By: Claude Opus 5 <[email protected]>
Runs the issue's own repro against the real stack — real Electron main
process, real Store/runner/registerIpc, real preload bridge in a real
BrowserWindow — with N concurrent CODER_SIMULATE runs (free, no network)
while hammering git:diff / git:status / files:list / git:syncInfo from the
renderer. Main process measures its own loop lag with the same perf_hooks
histogram CODER_LOOP_LAG uses.

Measured across the branch point, 4 threads / 15s / ~596 git IPC calls:
  before: lag p99 63.4ms, max 149.3ms, STARVED 60.4%
  after:  lag p99  2.0ms, max  33.6ms, STARVED  3.5%
At 8 threads: 60.7% -> 5.0%.

Sustained starvation is the beachball, and it is gone. Isolated ~60-90ms
spikes remain, consistent with the whole-store JSON.stringify (#225).

Co-Authored-By: Claude Opus 5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…when-multiple-threads-run-4e7132

# Conflicts:
#	electron/main.js
@currentbits
currentbits merged commit 79a1e52 into main Aug 17, 2026
1 check failed
currentbits pushed a commit that referenced this pull request Aug 17, 2026
…remainder

origin/main (#229) finished the sync-gh work this branch had duplicated, so
the overlapping half is dropped in favour of main's. What survives is exactly
what #228 asked for and main still does not have: resolveThreadGit and the
project default-branch lookup off sync git on the PR read paths.
@currentbits
currentbits deleted the coder/app-beachballs-when-multiple-threads-run-4e7132 branch October 3, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App beachballs when multiple threads run concurrently

2 participants