Repository navigation
perf(pty): cap the unsent main→renderer PTY backlog (Win/Linux GB-scale leak) - #7630
Conversation
The per-pty `pendingData` string in ipc/pty.ts (main's unsent main→renderer output queue) had no size cap. The 512 KB/pty + 8 MB in-flight caps bound only SENT-but-unacked bytes; `onData` appends to `pendingData` unconditionally and `flushPendingData` merely `continue`s (no drain) while ack-gated. On Windows/Linux the main renderer is background-throttled — `setBackgroundThrottling(false)` is set only for `process.platform === 'darwin'` — and Chromium freezes hidden pages after ~5 min, so a backgrounded Orca with an active/verbose agent stops ACKing: in-flight pins at 8 MB, the flush gates, and `pendingData` grows at raw PTY throughput → MB→GB in the main process. macOS is unaffected. Fix: cap the backlog to the most-recent PENDING_DATA_MAX_CHARS (2 MB, matching the daemon pendingOutput and renderer scheduler caps), advancing startSeq by the dropped-char count (the same arithmetic flushPendingData uses when slicing), and set a `droppedBacklog` flag on the next payload. The renderer's dataCallback sees the flag and calls the existing markHiddenOutputRestoreNeeded(), so on hidden→visible it rebuilds the dropped span from the main headless snapshot — no output is lost within scrollback depth. Background agents keep writing full-speed into the runtime buffer; only the redundant delivery copy is trimmed. The flag threads through the same layers as the existing `background` field: main payload → preload types → dispatcher PtyDataMeta → renderer. Tests (red→green): a daemon pty emits 5 MB while the renderer never ACKs. Without the cap the flag is never set (fail); with it the first emitted chunk carries droppedBacklog exactly once and delivery stays bounded. Ordinary small output never sets the flag. Note: leak + fix are Win/Linux-only (macOS is throttling-exempt), so not reproducible at runtime on macOS; evidence is the code trace + tests.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds a cap on per-PTY unsent backlog in the main process and introduces a Related issues: None provided. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…ot the cap proof Co-authored-by: Orca <[email protected]>
|
Adversarial review complete — shipping.
Leak engagement path is Win/Linux background-throttle only (not runtime-reproducible on macOS); a Win/Linux RSS soak with a chatty background agent remains worthwhile post-merge. |
46 commits from main. Conflict resolutions (branch structure wins, main semantics grafted): - pending-backlog cap (#7630): main's capPendingPtyData/droppedBacklog 2MB tail-trim EXCISED — this branch's droppedOutput sentinel mechanism (O(1) drop + query salvage + settings-scaled cap + restore marker) already bounds the unsent backlog and strictly supersedes it; carrying both would race two cap policies over one buffer. Kept main's makePtyDataPayload helper and exit-time pending flush (adapted to the sentinel). Main's duplicate never-ACK cap test removed (covered by the existing sentinel cap tests); its reload-gate tests kept with the runtime mock extended. - replay guard (#7661): main's blind REPLAY_GUARD_RELEASE_FALLBACK_MS release dropped — this branch's probe-certified stall release covers the same cold-restore reattach lockout without the auto-reply leak a timed release can cause in agent TUIs. Main's three regression tests KEPT, adapted to probe timing (explicit stallCheckMs). - hidden-pressure scenario: kept this branch's STRICT 1500ms restore budget (the background keep-tail global budget earns it; main relaxed to 4s for CI drain overhead) + grafted main's timer-drift gate const. - ssh-docker-relay-perf: kept this branch's ACK-stalled background PTY test AND grafted main's new file-streams+git-churn test (4 tests). - preload/renderer: droppedBacklog threading excised with the mechanism; main's unrelated changes (ssh importConfig reAdopt, etc.) kept. Gates: typecheck clean; ipc+daemon+terminal-pane+pane-manager suites 4,396 passed / 14 skipped. Co-authored-by: Orca <[email protected]>
#7630 landed the same 2MB cap + droppedBacklog restore flag this branch introduced independently — adopt its mechanism wholesale (constants, flag naming, renderer threading, cap tests). This branch keeps what #7630 does not cover: releasing the queue + in-flight ACK budget on teardown paths that skip sendPtyExitToRenderer (SSH connection drop, synthetic-kill dedup), and dropping renderer delivery for output arriving inside the synthetic-kill window. Co-authored-by: Orca <[email protected]>
…le leak) (stablyai#7630) * perf(pty): cap the unsent main→renderer PTY backlog (Win/Linux GB leak) The per-pty `pendingData` string in ipc/pty.ts (main's unsent main→renderer output queue) had no size cap. The 512 KB/pty + 8 MB in-flight caps bound only SENT-but-unacked bytes; `onData` appends to `pendingData` unconditionally and `flushPendingData` merely `continue`s (no drain) while ack-gated. On Windows/Linux the main renderer is background-throttled — `setBackgroundThrottling(false)` is set only for `process.platform === 'darwin'` — and Chromium freezes hidden pages after ~5 min, so a backgrounded Orca with an active/verbose agent stops ACKing: in-flight pins at 8 MB, the flush gates, and `pendingData` grows at raw PTY throughput → MB→GB in the main process. macOS is unaffected. Fix: cap the backlog to the most-recent PENDING_DATA_MAX_CHARS (2 MB, matching the daemon pendingOutput and renderer scheduler caps), advancing startSeq by the dropped-char count (the same arithmetic flushPendingData uses when slicing), and set a `droppedBacklog` flag on the next payload. The renderer's dataCallback sees the flag and calls the existing markHiddenOutputRestoreNeeded(), so on hidden→visible it rebuilds the dropped span from the main headless snapshot — no output is lost within scrollback depth. Background agents keep writing full-speed into the runtime buffer; only the redundant delivery copy is trimmed. The flag threads through the same layers as the existing `background` field: main payload → preload types → dispatcher PtyDataMeta → renderer. Tests (red→green): a daemon pty emits 5 MB while the renderer never ACKs. Without the cap the flag is never set (fail); with it the first emitted chunk carries droppedBacklog exactly once and delivery stays bounded. Ordinary small output never sets the flag. Note: leak + fix are Win/Linux-only (macOS is throttling-exempt), so not reproducible at runtime on macOS; evidence is the code trace + tests. * test(pty): clarify that the sent-total assertion is a sanity bound, not the cap proof Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Orca <[email protected]>
Description
The per-pty
pendingDatastring insrc/main/ipc/pty.ts— the main process's unsent main→renderer output queue — had no size cap. The existing 512 KB/pty and 8 MB-total caps bound only sent-but-unacked bytes (rendererInFlightCharsByPty); the queued-but-unsent backlog grows freely:onDataappends every PTY chunk viaappendPendingPtyData(plain string concat, no cap).flushPendingDatais ACK-gated bycanSendPtyDataToRenderer; when gated it justcontinues (does not drain).So the only thing bounding
pendingDatais the renderer sendingpty:ackData. On Windows/Linux the main renderer is background-throttled —createMainWindow.ts:307callssetBackgroundThrottling(false)only insideif (process.platform === 'darwin'), and Chromium freezes hidden pages after ~5 min. So a backgrounded Orca window with an active or verbose background agent stops ACKing → in-flight pins at 8 MB → the flush gates →pendingDatagrows at raw PTY throughput → MB→GB in the main process. macOS is unaffected (throttling disabled there).This is triggered by the extremely common act of backgrounding/minimizing the app while a background agent or build keeps printing.
Fix
Cap the per-pty backlog to the most-recent
PENDING_DATA_MAX_CHARS(2 MB, matching the daemonpendingOutputand renderer scheduler caps, both 2 MB), advancingstartSeqby the dropped-char count (the identical arithmeticflushPendingDataalready uses when it slices), and set adroppedBacklogflag on the next payload.The renderer's
dataCallbacksees the flag and calls the existingmarkHiddenOutputRestoreNeeded()— the same recovery the renderer's own 2 MB scheduler overflow already uses. On hidden→visible it rebuilds the dropped span from the main headless-emulator snapshot, so no output is lost within the pane's scrollback depth. The flag threads through the same layers as the existingbackground/seqfields: main payload → preload types → dispatcherPtyDataMeta→ one-line renderer trigger.Why this loses zero user-visible output: the leak scenario is intrinsically the hidden renderer path. Every PTY byte is independently fed to the runtime headless emulator (which owns the pane's real scrollback) regardless of renderer state, and the renderer already snapshot-reconciles on un-hide. Background agents keep writing full-speed into that buffer — only the redundant main→renderer delivery copy is trimmed, so agent throughput is unaffected (a PTY-pause fix would have stalled background agents; this does not).
Evidence (red → green)
Two new tests in
pty.test.tsdrive a daemon pty that emits 5 MB while the renderer never ACKs:AssertionError: expected undefined to be true—droppedBacklogis never set. Leak reproduced (backlog would grow unbounded).droppedBacklog: trueexactly once, and total delivered chars stay bounded. A second test asserts ordinary small output never sets the flag (no common-case regression). ✅Verification:
pty.test.ts— 217 tests green (2 new)pty-connection.test.ts(331) + hidden-output-restore + dispatcher suites greenoxlintclean,tsgonode and web typecheck cleanPlatform note
The leak and the fix are Windows/Linux-only (macOS is throttling-exempt via
setBackgroundThrottling(false)), so this is not reproducible at runtime on macOS (can't/electron-verify on a Mac). Evidence is the code trace + unit/integration tests. Worth a manual Win/Linux soak (background the app with a chatty background agent, watch main-process RSS) before/after.ELI5
When you hide Orca on Windows/Linux, the app's window "falls asleep" and stops saying "got it" to the part of the program feeding it terminal text. That feeder kept stuffing everything into a bag with no bottom — a busy background robot could fill it with gigabytes until the app ran out of memory. Now the bag has a 2 MB limit: when it's full we keep only the newest text and leave a note ("you missed some"). When you look at the window again, it rebuilds the missing part from a complete copy the app kept elsewhere — so you still see everything, and memory stays bounded.
Trade-offs / regression risk: none in normal use
pendingDataexceeds 2 MB, which only happens when the renderer isn't ACKing (throttled/frozen) — never in normal foreground operation, where the renderer ACKs fast and the backlog stays tiny. The "small output" test locks this in.Context
Second of two massive-scale leaks found this pass. The first — the native-chat transcript read-cache retaining full transcripts (GB-scale) — is #7625.
Made with Orca 🐋