Skip to content

perf(cli): strip Windows handle inheritance + fast daemon health probe (partial #91)#97

Merged
zackees merged 1 commit intomainfrom
perf/91-windows-daemon-spawn-handles
Apr 18, 2026
Merged

perf(cli): strip Windows handle inheritance + fast daemon health probe (partial #91)#97
zackees merged 1 commit intomainfrom
perf/91-windows-daemon-spawn-handles

Conversation

@zackees
Copy link
Copy Markdown
Member

@zackees zackees commented Apr 18, 2026

Summary

Two targeted warm-path fixes identified in the #91 profiling report. Both live in crates/fbuild-cli/src/daemon_client.rs.

F1 — Strip HANDLE_FLAG_INHERIT from CLI stdio before spawning daemon

spawn_daemon_process on Windows previously let the daemon grandchild inherit every inheritable handle in the CLI — including the parent shell's stderr pipe (attached via |, 2>&1, or PowerShell's default pipeline). The daemon holds that pipe open for SELF_EVICTION_TIMEOUT = 120 s, so the shell stays blocked until that timer fires even after the CLI's main() has returned.

Reproducibly visible as a ~125 s stall in PowerShell; adding 2>$null (PS) or 2>/dev/null (bash) eliminates the stall and drops total to ~4 s. Exact match to the 120 s eviction timeout was the smoking gun in #91's profiling.

Fix: call SetHandleInformation(GetStdHandle(STD_*_HANDLE), HANDLE_FLAG_INHERIT, 0) on the CLI's std handles immediately before cmd.spawn(). Rust's Command still duplicates the explicit Stdio handles it configured (null/null/log_file) as inheritable for the daemon, so stderr-to-log-file redirection is preserved; only the shell-owned pipe is stripped. Uses a tiny extern "system" FFI block — no new crate dependencies.

F2 — 100ms connect_timeout on DaemonClient

When the daemon is not running, TCP connect refusal returns instantly (<1 ms, verified with Test-NetConnection). reqwest's per-request .timeout(2s) would nevertheless wait the full 2 s before surfacing ECONNREFUSED. DaemonClient::new() is called from two sites (display_daemon_stats_compact + ensure_daemon_running), so the cold path eats 4 s before any work starts.

Fix: build the shared client with ClientBuilder::connect_timeout(Duration::from_millis(100)).

Measured

Scenario Before After
fbuild daemon status (daemon not running, bash) 4.0 s 0.28 s
fbuild build ... warm in PowerShell w/ attached stderr ~125 s expected ~2 s — live verification pending; needs hands-on PowerShell session

Test plan

  • uv run cargo build -p fbuild-cli --release clean.
  • uv run cargo clippy --workspace --all-targets -- -D warnings clean.
  • uv run cargo test -p fbuild-cli — 9/9 pass.
  • F2 empirically verified: cold daemon status 4.0 s → 0.28 s.
  • F1 empirically verified in PowerShell with the 125 s reproducer (needs a non-busy daemon; please verify on merge).

Scope

Related

…probe (partial #91)

Two targeted warm-path fixes identified during #91 profiling:

F1 — `spawn_daemon_process` on Windows previously let the daemon inherit
every inheritable handle in the CLI process, including the parent shell's
stderr pipe (attached via `|`, `2>&1`, or PowerShell's default pipeline).
The daemon holds that pipe open for SELF_EVICTION_TIMEOUT (120s), so the
shell stays blocked until the timer fires even after the CLI's main() has
returned — reproducibly visible as a ~125s stall in PowerShell.

Fix: call `SetHandleInformation(GetStdHandle(STD_*_HANDLE), HANDLE_FLAG_INHERIT, 0)`
on the CLI's std handles immediately before `cmd.spawn()`. Rust's Command
still duplicates the explicit Stdio handles it configured (null/null/log_file)
as inheritable for the daemon, so stderr-to-log-file redirection is
preserved; only the shell-owned pipe is stripped.

F2 — `DaemonClient::new()` now builds its reqwest client with a 100ms
`connect_timeout`. When the daemon is not running, TCP connect refusal
returns instantly on Windows/Linux, so waiting the full 2s `.timeout()`
of each probe just wastes wall-clock. Measured: `fbuild daemon status`
cold path dropped from 4.0s to 0.28s (2 probes × ~100ms).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 18, 2026

Warning

Rate limit exceeded

@zackees has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 50 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 5 minutes and 50 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a1e9aceb-e856-45d3-b2ac-eda77839fce4

📥 Commits

Reviewing files that changed from the base of the PR and between e08015e and 55673ef.

📒 Files selected for processing (1)
  • crates/fbuild-cli/src/daemon_client.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/91-windows-daemon-spawn-handles

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 and usage tips.

@zackees zackees merged commit 945bb74 into main Apr 18, 2026
75 of 77 checks passed
@zackees zackees deleted the perf/91-windows-daemon-spawn-handles branch April 18, 2026 16:06
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