Skip to content

preview-controller: output-stall watchdog measures progress, not frame start (#774) - #788

Merged
d-q222 merged 1 commit into
mainfrom
fix/774-helper-backpressure
Sep 17, 2026
Merged

d-q222 merged 1 commit into
mainfrom
fix/774-helper-backpressure

Conversation

@d-q222

@d-q222 d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #774. Supersedes #775, which only raised the timeout from 2 s to 10 s.

Problem

The stdout writer's stall watchdog timed each frame from the moment its write_all began. Under a slow but live reader, a reply of 500 KB or more legitimately takes longer than any fixed bound, and the watchdog killed the helper with "output stalled; delivery uncertain". LargeDocumentEditorTests hit this under CPU contention.

Fix

  • Raw fd: the writer writes to the raw stdout fd, bypassing the LineWriter that turned a whole frame into one opaque blocking call.
  • Chunked writes: each write(2) is at most 64 KB, and every accepted chunk resets the watchdog clock.
  • Timeout: OUTPUT_STALL_TIMEOUT is 10 s with no forward progress. A slow reader survives; a stuck one still trips the watchdog.
  • Docs: STDIO.md is updated.

Tests

  • cargo test --release in crates/preview-controller: 89 passed, 0 failed, 4 ignored.
  • New test: slow_but_progressing_reader_survives_a_reply_far_longer_than_the_watchdog paces a 2.2 MB reply over about 16 s and checks it arrives byte-exact.
  • Recalibrated: the two genuinely-stuck-reader tests now use 13 s and 14 s deadlines.
  • Mac app: LargeDocumentEditorTests.testLargePasteSelectionAndTypingEndDurableThroughTheRealHelper passed 3 of 3 times with this helper, with 20 CPU-bound processes running alongside.

Attribution

Implemented by a Claude Sonnet 5 lane under daniel-parent (Commander).

🤖 Generated with Claude Code

#774)

Root cause, confirmed against this branch's own repro (20 `yes` processes,
18-core machine): the per-write stall watchdog judged a frame's write by
elapsed time since the write *started*, using a single write_all() through
the process's buffered Stdout. Under reader backpressure a large reply
(500KB+) legitimately takes longer than any fixed threshold to drain, but
looks identical to a truly stuck pipe from that vantage point -- so PR #775's
"raise 2s to 10s" only delays the same failure to a bigger payload or a
busier machine, it doesn't fix the underlying blind spot.

Fix: the writer thread now writes to the raw stdout fd (bypassing the
LineWriter, which would otherwise swallow a whole line-terminated frame into
one opaque blocking call) in bounded WRITE_CHUNK pieces, resetting the
watchdog's clock on every chunk that makes forward progress. A blocking pipe
write does not return a short count under backpressure, so chunking the
*request* is what keeps control returning to the watchdog periodically. A
slow-but-alive reader now keeps resetting the clock indefinitely; a reader
that never drains anything still trips OUTPUT_STALL_TIMEOUT exactly as
before. Verified against the pre-existing intentionally-never-read tests
(now recalibrated to the shared 10s bound, matching PR #772's family), plus
a new test that reads a 2.2MB reply in artificially slow, steadily-paced
chunks totaling ~16s -- longer than the watchdog bound -- and asserts the
helper stays alive and delivers the reply byte-exact.

Also ran LargeDocumentEditorTests.testLargePasteSelectionAndTypingEndDurableThroughTheRealHelper
3x under real 20-process CPU contention with this branch's preview-controller
and the fix681 compiler: all three passed (6.8s/7.3s/7.2s), no stalls.

crates/preview-controller/STDIO.md updated to describe the progress-based
bound instead of the stale "two seconds" line.

Implementation-Agent: claude-sonnet-5 subagent of daniel-parent (Commander)
Commit-Executor: daniel-parent subagent
Co-authored-by: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222
d-q222 merged commit 79fafbd into main Sep 17, 2026
21 of 22 checks passed
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.

LargeDocumentEditorTests: helper exited (1): output stalled; delivery uncertain (newly reachable failure)

1 participant