Skip to content

fix(process): bound the synchronous macOS ps reads that stall the server - #1748

Merged
devswha merged 2 commits into
mainfrom
fix/process-start-sync-bound
Oct 23, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/process-start-sync-bound

Conversation

@devswha

@devswha devswha commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Change

#1404 captured the server's main thread on macOS waiting inside Bun.spawnSync while herdr itself answered in under a millisecond. server/process-start.ts runs /bin/ps synchronously on macOS with no timeout, on paths that run per request. Every synchronous probe there now has a 1 s timeout (then SIGKILL), and a timed-out probe answers null, 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) and server/usage.ts mkfifo (unbounded, but another open PR holds that file).

Refs #1404

Validation

  • server/process-start.test.ts with an injected spawn: a timeout gives null; pending-input and outcome tests still pass (145). A null birth alone is not treated as the process being gone by the pending-message path.
  • A real hung subprocess returns null after about 1,002 ms. Native macOS was not run.
  • bun run check fast: pass.

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
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between c9b8dbb and c10ad42.

📒 Files selected for processing: 3
  • changelog.d/process-start-sync-bound.md
  • server/process-start.test.ts
  • server/process-start.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • On macOS, a stalled process-start check can no longer block conversation and pending-send checks indefinitely; it now has a one-second limit.
    • When the check times out or cannot determine a valid start time, the app handles the result without treating it as a confirmed process start. Linux behavior is unchanged.

The macOS process-start functions now use a shared reader that limits /bin/ps calls to one second. The reader returns null for failed or invalid reads. Linux behavior remains unchanged.

Changes: macOS process-start reader

Layer / File(s) Summary
Implement and test the bounded reader
server/process-start.ts, server/process-start.test.ts, changelog.d/process-start-sync-bound.md
The shared reader applies a one-second timeout and SIGKILL. Tests cover failures, probe arguments, and clock values. The changelog describes the limit.
Use the reader for macOS process starts
server/process-start.ts
processStartedAt and processStartLowerBound use the shared reader instead of separate synchronous probes.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • devswha/herdr-web-ui#1545 — The change bounds synchronous ps reads but does not remove them, so it partially addresses the issue’s objective.

Suggested reviewers: jiaoew1991

Merge Risk: ⚪ Minimal · up to c10ad

The macOS process-start probes are bounded as intended. No identified issue prevents merging after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description check Passed The description includes the required Change and Validation sections. It explains the macOS timeout behavior, affected paths, deferred asynchronous work, references, test coverage, validation results,…
Title check Passed The title clearly and concisely describes the main change: bounding synchronous macOS ps reads that can stall the server.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description includes the required Change and Validation sections. It explains the macOS timeout behavior, affected paths, deferred asynchronous work, references, test coverage, validation results, and the limitation that native macOS was not run.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 23, 2026

Copy link
Copy Markdown
Owner Author

Merge pass: reviewed instead of a CodeRabbit review (rate-limited). A gate reviewer on a different model from the author's reported:

  • Timeout: every synchronous macOS process-start probe now uses a 1 s timeout and SIGKILL. A real hung child ended after about 1.0 s on Linux and on macOS (eamac).
  • Timeouts and queued input: a timeout answers null, and pending-input identity checks treat null as unknown rather than gone, so queued input still waits for the user.
  • Tests: Linux 178 pass; native macOS 8 pass with 3 Linux-only skips.
    All required checks are green and no review thread is open.

@devswha
devswha merged commit 8c7eec1 into main Oct 23, 2026
17 checks passed
@devswha
devswha deleted the fix/process-start-sync-bound branch October 23, 2026 10:05
devswha pushed a commit that referenced this pull request Oct 26, 2026
… 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
devswha pushed a commit that referenced this pull request Oct 26, 2026
… 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
devswha pushed a commit that referenced this pull request Oct 26, 2026
… 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
devswha added a commit that referenced this pull request Oct 26, 2026
… 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>
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.

1 participant