Repository navigation
fix(process): bound the synchronous macOS ps reads that stall the server - #1748
Conversation
Limit macOS ps reads to one second, preserve unknown results on timeouts, and coalesce repeated reads with a 128-entry LRU and a fixed 100 ms lifetime. Keep Linux identity reads fresh and invalidate cached processes observed gone. Refs #1404
Read process starts afresh instead of accepting signal-0 liveness as process identity. Keep the one-second synchronous probe timeout, SIGKILL, and unknown-on-failure behavior. Remove cache coverage and correct the changelog. Refs #1404
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Summary
Merge Risk: ⚪ Minimal · up to
|
|
Merge pass: reviewed instead of a CodeRabbit review (rate-limited). A gate reviewer on a different model from the author's reported:
|
… and await name lookups' git #1404 sampled the server's main thread on macOS waiting inside Bun.spawnSync while herdr answered in under a millisecond. #1748 bounded those waits at 1 s; they still held every connection while they ran. - process-start.ts, gjc-runtime.ts and hermes.ts no longer run ps on macOS: the process's start and controlling terminal come from the kernel's kinfo_proc (sysctl KERN_PROC_PID and devname through libSystem), the read claude-store.ts already made for Claude. It stays synchronous, so no caller and no unknown-identity rule changes, and the start is exact rather than ps's whole seconds. The sysctl helper moves to darwin-process.ts. - files.ts: filesNamed awaits the streaming, capped and deadlined git listing paneFiles already used, instead of Bun.spawnSync for up to 3 s; locateFile follows. - usage.ts: the keychain FIFO's mkfifo is awaited and killed after 5 s. Startup-only probes stay synchronous: process-owner.ts (owner guard, persisted lstart birth), managed.ts, remote-entry.ts, pty/host.ts and pty/sidecar.ts. Refs #1404, #1545
… and await name lookups' git answered in under a millisecond. #1748 bounded those waits at 1 s; they still held every connection while they ran. - process-start.ts, gjc-runtime.ts and hermes.ts no longer run ps on macOS: the process's start and controlling terminal come from the kernel's kinfo_proc (sysctl KERN_PROC_PID and devname through libSystem), the read claude-store.ts already made for Claude. It stays synchronous, so no caller and no unknown-identity rule changes, and the start is exact rather than ps's whole seconds. The sysctl helper moves to darwin-process.ts. - files.ts: filesNamed awaits the streaming, capped and deadlined git listing paneFiles already used, instead of Bun.spawnSync for up to 3 s; locateFile follows. - usage.ts: the keychain FIFO's mkfifo is awaited and killed after 5 s. Startup-only probes stay synchronous: process-owner.ts (owner guard, persisted lstart birth), managed.ts, remote-entry.ts, pty/host.ts and pty/sidecar.ts. Refs #1404, #1545
… and await name lookups' git answered in under a millisecond. #1748 bounded those waits at 1 s; they still held every connection while they ran. - process-start.ts, gjc-runtime.ts and hermes.ts no longer run ps on macOS: the process's start and controlling terminal come from the kernel's kinfo_proc (sysctl KERN_PROC_PID and devname through libSystem), the read claude-store.ts already made for Claude. It stays synchronous, so no caller and no unknown-identity rule changes, and the start is exact rather than ps's whole seconds. The sysctl helper moves to darwin-process.ts. - files.ts: filesNamed awaits the streaming, capped and deadlined git listing paneFiles already used, instead of Bun.spawnSync for up to 3 s; locateFile follows. - usage.ts: the keychain FIFO's mkfifo is awaited and killed after 5 s. Startup-only probes stay synchronous: process-owner.ts (owner guard, persisted lstart birth), managed.ts, remote-entry.ts, pty/host.ts and pty/sidecar.ts. Refs #1404, #1545
… and await name lookups' git (#1980) ## Change #1404 sampled the server's main thread on macOS waiting inside `Bun.spawnSync` while herdr answered in under a millisecond. #1748 bounded those waits at 1 s, but each one still held every connection while it ran. This change removes the synchronous subprocess calls from the paths that run per request or per status poll: - **macOS process metadata no longer runs `ps`.** `process-start.ts` (`processStartedAt`, `processStartLowerBound`, `processStartUpperBound[Async]`), `gjc-runtime.ts` `gjcTerminal` and `hermes.ts` `hermesTerminalId` read the process's start and controlling terminal from the kernel's `kinfo_proc` (`sysctl KERN_PROC_PID`, plus `devname(3)` for the tty), through `bun:ffi` on libSystem. `claude-store.ts` already made the same read for Claude on macOS; the helper moves to `server/darwin-process.ts`. The read takes microseconds and starts no process, so it **stays synchronous**: no caller changes, and no new "unknown" state reaches OmO resolution or pending input (the reason #1407 was closed). The start is now exact rather than `ps`'s whole seconds, and every comparison keeps its tolerance. - **`files.ts` `filesNamed`** (opening a file an agent named without its folder) awaits the streaming git listing `paneFiles` already used, which has the same 3 s deadline, `SIGKILL` and 16 MiB cap, instead of `Bun.spawnSync`. `file-view.ts` `locateFile`/`locateFileInFolder` become async; `findFile`, their only production caller, already was. - Lookups for one folder that arrive while its git listing runs share that listing, instead of each starting a git (the old synchronous lookup ran them one after another). - **`usage.ts`**: the keychain FIFO's `mkfifo` is awaited and killed after 5 s. - **CI**: the macOS job also runs `server/darwin-process.test.ts`, the only check that the kernel record's terminal field matches `ps`. This is a workflow change. Still synchronous, on purpose, because each runs once at startup or inside the owner guard, not per request: `process-owner.ts` (owner guard; its persisted `lstart` birth format is compared across restarts), `managed.ts` (`git rev-parse` before the supervisor starts), `remote-entry.ts` (Windows marker at bridge start), `pty/host.ts` and `pty/sidecar.ts` (PTY host detection). Refs #1404 (it also asks about the reporter's remaining stalls, which #1407's comments tie to a `Background` LaunchAgent `ProcessType`; this PR does not claim to close that). Covers the macOS half of #1545's "no spawnSync reachable from currentIdentity". ## Validation - **eamac** (macOS 27.0.1 arm64, Bun 1.4.3), a fresh clone of origin/main `7ab4bde0b` and then the same clone with this patch. A script runs the four metadata reads a status poll makes (`processStartedAt`, `processStartLowerBound`, `hermesTerminalId`, `gjcTerminal`) in a loop for 4 s. Meanwhile a 2 ms timer measures event-loop gaps and a separate process sends HTTP requests to the same server every 20 ms: | | one round of reads, p50 | longest timer gap | HTTP p50 / max | |---|---|---|---| | main | 6.6–6.8 ms | 14.6–20.7 ms | 2.7–3.0 / 14.5–14.8 ms | | this PR | 0.06 ms | 2.6 ms | 0.2 / 0.7 ms | | main, `taskpolicy -b` (background QoS, as the reporter's LaunchAgent ran) | 27.6–29.6 ms | 52.5–53.2 ms | 9.9–11.3 / 48–52 ms | | this PR, `taskpolicy -b` | 0.47 ms | 11.5 ms (69 ms once) | 0.6–0.7 / 10.6 ms (112 ms once) | The two background-QoS outliers come from the scheduler throttling a busy loop; no subprocess is started. - New `server/darwin-process.test.ts` checks the `e_tdev` offset and the NODEV/short-answer cases. On macOS it also checks that the kernel's start and tty match what `ps -o tty= -o lstart=` prints for a live child, and that a missing pid gives null. The kinfo_proc layout was checked with clang on eamac: `sizeof(struct kinfo_proc)` = 648, `offsetof(kp_eproc.e_tdev)` = 572. - New `files.test.ts` case: while a fake git blocks on a FIFO, `filesNamed` lets a timer run before it answers, and a second lookup of the same folder shares that one git (one `Bun.spawn`; without the sharing it makes two, checked). Rewiring `filesNamed` to `Bun.spawnSync` makes that test fail (checked, then restored). The existing "both inventories" test now asserts that `Bun.spawnSync` is never called. - macOS focused suites on eamac (darwin-process, process-start, claude-store, gjc-runtime, files, file-view, hermes, grok, muse-store, usage): 342 pass, 7 skip, 1 fail. The failure is #1971, which fails the same way on unchanged main (3 of 3 runs). - Linux focused suites: 347 pass, 3 skip, 0 fail. - `bun run check fast` on the final head (5f8b825, rebased on main after #1986): all pass, unit 7261 pass, 0 fail, 25 skip. On eamac with a real tty, the macOS process tests give 299 pass, 3 skip, 0 fail. - Reviews: omo-native-code-reviewer: APPROVE, no blockers. Its findings and how each was resolved: - MEDIUM: `darwin-process.test.ts` did not run in the macOS CI job. Fixed: `ci.yml` runs it. - LOW: comments still gave `ps`'s whole seconds as the reason for the macOS start tolerances. Fixed: reworded; the tolerances are unchanged. - LOW: concurrent lookups of one folder each started their own git. Fixed: one shared listing per folder. - LOW: the offset unit test writes at the offset the code reads, so it cannot catch a wrong offset. Covered by the macOS CI test, which compares with `ps`. - NIT: `sysctlCall()` makes a closure per call. Kept: negligible. - Codex Sol review did not run (host stall); the merge pass reviews. - Astra gate (merge pass) B1, PID reuse could pair one process's terminal with another's start: fixed in 4d5ac0d. `darwinTerminal` returns the tty and the start from one `kinfo_proc`, and `gjcTerminal` and Hermes's breadcrumb lookup take both from it. A new test feeds two records to one lookup and checks that only the first is read; going back to two reads fails it (checked). On eamac with a real terminal (`ssh -tt`, `/dev/ttys001`), the tty and start match `ps`; darwin-process, gjc-runtime, hermes and process-start tests: 56 pass, 3 skip, 0 fail. --------- Co-authored-by: mp <mp@local>
Change
#1404 captured the server's main thread on macOS waiting inside
Bun.spawnSyncwhile herdr itself answered in under a millisecond.server/process-start.tsruns/bin/pssynchronously on macOS with no timeout, on paths that run per request. Every synchronous probe there now has a 1 s timeout (thenSIGKILL), and a timed-out probe answersnull, exactly like a failed read today, so no caller changes. Every read stays fresh (no caching, so a reused pid can never get an old start time).Moving the callers to an asynchronous read is #1545 and is not done here. Other synchronous subprocess calls found on request paths:
server/files.ts(already bounded at 3 s) andserver/usage.tsmkfifo(unbounded, but another open PR holds that file).Refs #1404
Validation
server/process-start.test.tswith an injected spawn: a timeout givesnull;pending-inputandoutcometests still pass (145). A null birth alone is not treated as the process being gone by the pending-message path.nullafter about 1,002 ms. Native macOS was not run.bun run check fast: pass.