Files
museion/docs/devlog/20261117-acp-backpressure-defeats-timeout-and-shutdown_log.md
T
AlexKucera ae3b99c7ea
CI / ci (pull_request) Canceled after 0s
CI / ci (push) Canceled after 0s
docs: brief-27 session log, INDEX row, changelog entry
Docs-only (code identical to the fix commit 89cd377, where the
697/697 suite ran): devlog carries the site-class enumeration with
disproofs, the decisions table, and the gates; CHANGELOG gains the
user-facing acp backpressure entry.
2026-11-17 22:36:56 +01:00

9.9 KiB
Raw Blame History

ACP backpressure — a blocked stdin writer can no longer defeat deadlines, Stop, or close

Date: 2026-11-17 Type: fix (availability — APP-DOWN class) Reference: brief-27 museion/acp-backpressure-defeats-timeout-and-shutdown (unticketed; argus cycle-4 dedicated bugs pass, finding 3 — ~/argus-audits/audit/2026-11-16/museion-bugs.md). Base bb4bfa2675c3fd2776bd0235c01010baec2290df (museion main tip at claim, re-verified via tea api + fetch). Scan SHA 11b196d6 — both touched source files byte-identical between scan SHA and base.

Finding

A hung engine that stops reading stdin fills the 64 KB pipe, and ACPEngineClient.send's synchronous fileHandleForWriting.write blocked the client's ONE serial queue — the same queue that carries request-id allocation, pending-registration, the asyncAfter deadline, and close()'s opening queue.sync barrier. A blocked writer therefore defeated the request timeout (the deadline sat queued behind the stuck send), starved Stop/lifecycle calls (a follow-up request hung at id-allocation queue.sync), and made close() block behind the write before SIGTERM/SIGKILL was ever reachable. EngineHost's close callers (killChat, withOneShot defer, shutdownAll) run on MainActor — engine backpressure became an app-wide unbounded hang on restart/quit. Argus executed the repro standalone: complete HEAD ACPEngineClient.swift in an isolated process, child argv swapped to /bin/sleep 30 (inert non-reading child), 2M-char request at timeout 0.1 s — no request completion, and an independent watchdog's close() never returned (probe killed at 12 s).

What Was Done

  • Fix (Museion/ACPEngineClient.swift, +34/−8): stdin writes — and the stdin write-handle's own close — moved off the state queue onto a dedicated serial writerQueue. request/notify/dispatch's server-reply all funnel through it (wire order preserved: one serial FIFO queue, same enqueue-order semantics as before). The state queue is now I/O-free: id allocation, registration, deadlines, and close()'s bookkeeping barrier always run; close() then does SIGTERM → 1 s grace → SIGKILL → reap without ever queueing behind the writer. The stdin write-handle close rides the writer queue (a blocked write may still own the handle — closing under a live write would race Foundation); it unblocks with EPIPE once the child's read end died, which the kill just arranged. Reader handles close on the caller as before (handlers off first, no writers exist for them).
  • Rig (MuseionTests/ACPClientTests.swift): two new zsh-fake modes. neverRead — execs /bin/sleep 30 without reading a line (the probe's inert non-reading child; exec keeps sleep the DIRECT child so close's SIGTERM reaches it and no orphan holds the pipe); serverPing — full behaviour plus one server→client request (id 900) emitted at initialize, logging our reply line to <name>-reply-log (the reply-delivery observable).
  • Smokes (MuseionTests/EngineSmokeTests.swift, +4): (1) request deadline fires while the stdin write is blocked (0.5 s deadline, 2M-char prompt, 5 s bounded window); (2) Stop accepted + bookkeeping answers while blocked (session/cancel notify returns, a follow-up request registers and reaches its OWN deadline); (3) close returns bounded while blocked (5 s window) and reaps the child; (4) healthy contract unchanged — round trip (initialize → 9.9.9), -32601 reply delivery over the real wire, idempotent clean close with reap. awaitBounded helper = the bounded window (house one-deadline rule); teardown in every blocked-writer smoke SIGKILLs the rig child FIRST so the base tree also settles without hanging the runner.

Site-class enumeration (mandate — re-derived, seed not ceiling)

Included (fixed): all four send call sites (request L177-ish, notify L197-ish, dispatch server-reply L317-ish — the seeded set) plus the deadline scheduling and both request-path queue.syncs and close()'s barrier — all healed by the same writer-queue separation; the direct server-request reply path rides the same send (no second site to mint, as the brief noted).

Disproved with evidence:

  • EngineGate.execute pre-spawn stdin write (EngineGate.swift:99-110): not queue-bound, and the write lands BEFORE process.run() — the reader does not exist yet, so it cannot block behind a hung engine; the in-source ceiling note (keep payloads well under 64 KB) plus the watchdog own it.
  • EngineGate.terminateAndReap (EngineGate.swift:145-155): SIGTERM → grace → SIGKILL → reap with NO preceding queue.sync — nothing can block before it.
  • ReadingPresenter apex render (ReadingPresenter.swift:137-154): a one-shot synchronous render helper (write → closeFile → waitUntilExit is the operation itself); no transport/lifecycle queue, no deadline machinery being defeated — adjacent, out of class.
  • MCPServer fflush(stdout) (MCPServer.swift:39): the app IS the child there; no engine pipe parenting — out of class.
  • ScriptedEngine (MuseionTests): mirrors the client's queue shape exactly, but performs zero FileHandle I/O (in-process) — its queue.syncs can never block on a pipe.
  • EngineHost seeded sites (killChat 175-178, withOneShot defer 204-207, shutdownAll 223-227): synchronous close callers; post-fix they call a bounded close (bookkeeping sync on an I/O-free queue; ≤1 s SIGTERM-grace worst case + reap). No EngineHost change required — see Decisions.

Gates

  • Focused serialized EngineSmokeTests at base (RED commit 33b7fe0): 6 executed / 3 passed / 3 failed — exactly the three blocked-writer smokes failed, on mechanism (deadline expectation nil after 5 s; bookkeeping expectation nil; finished != nil false). Healthy-contract smoke + both existing surface smokes passed at base.
  • Focused serialized EngineSmokeTests at fix (89cd377): 6 executed / 6 passed / 0 failed (exit 0). Per-test: deadline 0.539 s, stop/bookkeeping 0.733 s, close bounded 0.232 s, healthy 0.195 s.
  • Full suite at fix head via the rail (run dir 20261117T212755Z-Museion-serialized): 697 executed / 697 passed / 0 failed / 0 skipped, xcodebuild exit 0 (xcresult-authoritative; the four new smokes' started/passed lines verified in the run log).
  • swiftformat 0/149 files formatted; swiftlint zero violations (repo gate is zero, not net-zero — base was clean).
  • GitNexus: analyze --skip-agents-md in the worktree (AGENTS.md untouched); impact on every edited symbol — send LOW (5), dispatch LOW (3), notify LOW (0), close LOW (0), request UNKNOWN/0 (callers ride the EngineTransport existential; graph doesn't connect protocol witnesses) — no HIGH/CRITICAL, no user warning owed; detect-changes at the fix: 1 file / 8 symbols / 1 affected process (the transport), medium, expected.

Decisions & Rationale

Decision Rationale
Dedicated serial writer queue, blocking writes kept Boring over clever: nonblocking fd + DispatchSourceWrite drain would add partial-write buffering and source lifecycle for zero additional contract (the API-level guarantees all hold once the state queue is I/O-free). A blocked writer now wedges only itself — it cannot pin a deadline, Stop, or close.
stdin write-handle closes ON the writer queue After kill+reap a blocked write may still be in write(2) (unblocks on read-end death); Foundation FileHandle is not safe under concurrent write+close. The close job queues behind the last write — close() itself has already returned. Residual: a pathologic grandchild holding the pipe's read end past the child's death pins one writer-queue worker until it exits; the client is closed and reaped regardless.
exec /bin/sleep in neverRead (not sleep 30 as a zsh child) exec makes sleep the direct child: close's SIGTERM reaches it, and no orphan holds the pipe open — the smoke's teardown leaves nothing behind on either tree.
Case (2) pinned as accepted+unstarved, not bytes-delivered The bytes cannot reach a non-reading child — the pipe is full; that is physics, identical before and after. "Deliverable" means the seam accepts the notification and lifecycle bookkeeping still answers; the RED leg (follow-up request registration starving at queue.sync) is the un-fixed behavior.
EngineHost left untouched; close stays synchronous but bounded The fix direction's "no MainActor-blocking close path remains" read against the finding (backpressure → unbounded hang): the writer can no longer defeat close, and close is bounded (ms when the child honors SIGTERM; ≤ ~1 s grace + reap worst case). Making killChat/shutdownAll hop off MainActor would drop the reaped-before-return guarantee the teardown smokes pin (and the quit path relies on) — an EngineHost API change out of this finding's scope, disclosed to the dispatcher instead.
Zsh fake modes, never a real profile Brief's probe discipline: the only child ever spawned by the dead-child tests is our own zsh fake execing /bin/sleep (pre-validated standalone: ps shows /bin/sleep, a 2 MB stdin write blocks, serverPing round-trips the -32601 reply).

Disclosures

  • First focused RED run died at COMPILE (my Task<String?, Never> made awaitBounded's T? a double optional — two error: lines, zero tests executed, the rail's vacuous guard caught it as 0-executed/exit 65). Fixed to Task<String, Never>; the recorded RED run is the second, clean one.
  • No testmanagerd wedge encountered this brief (the known hazard did not fire; runs sequenced behind @athena's classis brief-26 verify, which finished 22:21:15 exit 0 before my first xcodebuild).
  • AGENTS.md DOX updates (Museion/AGENTS.md ACP-bridge bullets, MuseionTests/AGENTS.md suite-map lines) are consent-gated → owner-apply patch beside the handoff, git apply --check clean vs branch head; the root AGENTS.md session-log 10-row window is left for the write-log tooling (INDEX.md carries the durable row).
  • Docs commit (CHANGELOG + devlog + INDEX) is docs-only; the 697/697 suite ran at its parent (the fix commit) — code identical.