Skip to content

feat(daemon): process containment via running-process (closes #32)#108

Merged
zackees merged 1 commit intomainfrom
feat/process-containment-issue-32
Apr 18, 2026
Merged

feat(daemon): process containment via running-process (closes #32)#108
zackees merged 1 commit intomainfrom
feat/process-containment-issue-32

Conversation

@zackees
Copy link
Copy Markdown
Member

@zackees zackees commented Apr 18, 2026

Closes #32 — final piece of the daemon-teardown-hardening tracker. PRs #61 (Windows console ctrl handler) and #87 (SO_LINGER 0) landed the previous two.

Changes

  • Add running-process-core git dep (not on crates.io as of 2026-04-18):
    running-process-core = { git = "https://github.com/zackees/running-process", rev = "ff9d7972504f7a0dcee0f410274daf3b02e4fcc2", package = "running-process-core" }
    
  • New fbuild_core::subprocess::run_command helper — single chokepoint covering ~70 callers across fbuild-build (every platform orchestrator), fbuild-deploy (esptool/avrdude/teensy_loader/reset), fbuild-serial (addr2line), fbuild-packages/library_compiler.
  • fbuild-daemon/src/handlers/emulator.rs: contained find_node, ensure_avr8js_npm, find_simavr (blocking path) + run_avr8js_headless, run_qemu_process (tokio path).
  • fbuild-build/src/script_runtime.rs — extra_scripts python runtime.
  • fbuild-packages/src/disk_cache/budget.rs — powershell/df probes.

Intentional detached holdouts (inline-commented with INTENTIONALLY DETACHED (FastLED/fbuild#32))

  • fbuild-cli/src/daemon_client.rs::spawn_daemon_process — CLI→daemon; daemon must outlive CLI
  • fbuild-python/src/lib.rs::Daemon::ensure_running (sync + async) — Python→daemon, same
  • fbuild-build/src/zccache.rs::ensure_running — zccache is its own daemon

Integration test

crates/fbuild-daemon/tests/process_containment.rs + helper bin crates/fbuild-daemon/src/bin/containment_harness.rs:

  • Parent installs containment, spawns contained child, child spawns grandchild
  • Test hard-kills parent via taskkill /F (Windows) / SIGKILL (Unix)
  • Asserts both child and grandchild PIDs disappear within 10 s
  • Marked #[ignore] like port_recovery.rs; picked up by uv run test --full (which passes --include-ignored)

Test plan

  • uv run cargo test -p fbuild-daemon --test process_containment -- --include-ignored passes on Windows
  • uv run cargo clippy --workspace --all-targets -- -D warnings clean
  • uv run cargo fmt --all -- --check clean
  • CI: Linux + macOS coverage — upstream running-process-core uses verbatim pre_exec + killpg + PR_SET_PDEATHSIG for POSIX; CI confirms across platforms

🤖 Generated with Claude Code

@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 45 minutes and 18 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 45 minutes and 18 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: afe8b662-5271-47ca-8ec1-a4171e026654

📥 Commits

Reviewing files that changed from the base of the PR and between 8b1c261 and cfbfec2.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (16)
  • Cargo.toml
  • crates/fbuild-build/src/script_runtime.rs
  • crates/fbuild-build/src/zccache.rs
  • crates/fbuild-cli/src/daemon_client.rs
  • crates/fbuild-core/Cargo.toml
  • crates/fbuild-core/src/containment.rs
  • crates/fbuild-core/src/lib.rs
  • crates/fbuild-core/src/subprocess.rs
  • crates/fbuild-daemon/Cargo.toml
  • crates/fbuild-daemon/src/bin/README.md
  • crates/fbuild-daemon/src/bin/containment_harness.rs
  • crates/fbuild-daemon/src/handlers/emulator.rs
  • crates/fbuild-daemon/src/main.rs
  • crates/fbuild-daemon/tests/process_containment.rs
  • crates/fbuild-packages/src/disk_cache/budget.rs
  • crates/fbuild-python/src/lib.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/process-containment-issue-32

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 force-pushed the feat/process-containment-issue-32 branch from 5026916 to 6b1345e Compare April 18, 2026 17:51
@zackees zackees moved this to Triage in FastLED Tracker Apr 18, 2026
Every subprocess the fbuild daemon spawns now dies when the daemon dies,
including grandchildren forked by the child. On Windows this is a single
Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; on Linux it's
setpgid(0,0) + PR_SET_PDEATHSIG(SIGKILL); on macOS it's a process group
with a drop-time killpg. Eliminates the orphaned bash.exe / conhost.exe /
OpenConsole.exe (and Unix init-reparented) leaks after a daemon crash.

Dep: running-process-core via git, pinned to
ff9d7972504f7a0dcee0f410274daf3b02e4fcc2 (not yet on crates.io).

New module `fbuild_core::containment` exposes:
- init_global_containment() — called from fbuild-daemon/src/main.rs
- spawn_contained() / spawn_detached() for std::process::Command
- tokio_spawn::spawn_contained() for tokio::process::Command
  (Unix: pre_exec; Windows: parallel Job Object since running-process-core
  doesn't expose its handle to tokio Commands)

Spawn sites rerouted through containment:
- fbuild_core::subprocess::run_command — the central blocking helper,
  covering compilers, linkers, esptool, avrdude, addr2line, teensy_loader
  and every other tool launched via run_command across fbuild-build,
  fbuild-deploy, fbuild-serial, fbuild-packages (~70 call sites).
- fbuild-daemon/src/handlers/emulator.rs — find_node / ensure_avr8js_npm
  / find_simavr (run_command) and run_avr8js_headless / run_qemu_process
  (tokio_spawn::spawn_contained).
- fbuild-build/src/script_runtime.rs — extra_scripts python runtime.
- fbuild-packages/src/disk_cache/budget.rs — powershell/df probes.

Intentional hold-outs (documented inline):
- fbuild-cli/src/daemon_client.rs::spawn_daemon_process — the CLI spawns
  the daemon and exits; the daemon must outlive the CLI.
- fbuild-python/src/lib.rs::Daemon::ensure_running (×2) — same pattern
  from Python host.
- fbuild-build/src/zccache.rs::ensure_running — zccache is its own
  long-running daemon with independent lifecycle.

Integration test: crates/fbuild-daemon/tests/process_containment.rs
spawns a parent harness that installs containment, spawns a child which
spawns a grandchild, hard-kills the parent, then asserts both child and
grandchild PIDs disappear within 10 s. Passes on Windows; Unix uses
libc::kill probes, Windows uses OpenProcess + GetExitCodeProcess.

Verified: `uv run cargo check --workspace --all-targets`,
`uv run cargo clippy --workspace --all-targets -- -D warnings`,
`uv run cargo fmt --all --check`, full `uv run cargo test --workspace`,
and `cargo test -p fbuild-daemon --test process_containment --
--include-ignored`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@zackees zackees force-pushed the feat/process-containment-issue-32 branch from 6b1345e to cfbfec2 Compare April 18, 2026 17:59
@zackees zackees merged commit 49307a3 into main Apr 18, 2026
11 of 76 checks passed
zackees added a commit that referenced this pull request Apr 18, 2026
Publishes the fbuild-hash cache key pinning (#110) and process
containment (#108) to consumers (FastLED is testing QEMU emulator
builds against the PyPI distribution).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 19, 2026
…arm build

On Unix, `fbuild_core::containment::spawn_contained` delegated to
`ContainedProcessGroup::spawn_with_containment` from the
`running-process-core` crate. That implementation stores the first
spawned child's PID as the group's PGID and then has every subsequent
child call `setpgid(0, first_child_pid)` from its `pre_exec` hook.

Once the first child exits and is reaped (e.g. the short
`avr-gcc -dumpversion` call emitted by `log_toolchain_version`), the
kernel tears down that process group. The second spawn's
`setpgid(0, stale_pgid)` then fails with EPERM, which surfaces as
`build error: build failed: io error: Operation not permitted (os
error 1)` immediately after the `Toolchain: avr-gcc 7.3.0` line —
exactly the failure reported in #129.

This is reproducible on Linux CI from 2.1.17 onwards (Build Leonardo
et al.) and blocks every AVR / ESP32 / etc. build made via the daemon.

Fix: bypass the shared-pgid behaviour on Unix. Install a per-child
`pre_exec` hook that creates a fresh process group with
`setpgid(0, 0)` and, on Linux, requests `PR_SET_PDEATHSIG(SIGKILL)` so
the kernel still kills the child when the spawning daemon thread
exits. Windows is unchanged — Job Object assignment is stateless and
has no analogous failure mode. macOS loses the drop-time `killpg`
backstop, which was already a no-op in practice because the global
`ContainedProcessGroup` lives in a `OnceLock` that never drops; this
is the same coverage profile as before the fix.

Regression test: `sequential_contained_spawns_do_not_fail_with_eperm`
in `crates/fbuild-core/src/containment.rs` initialises the global
group and performs two consecutive `spawn_contained` calls with a
wait+sleep between them, mirroring the AVR build's "dumpversion then
compile" shape.

Refs #129. Reproducing commits: #108 (containment feature) +
the interaction surfaced by #120 / #119 that made the second-spawn
path universally reachable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 19, 2026
…arm build

On Unix, `fbuild_core::containment::spawn_contained` delegated to
`ContainedProcessGroup::spawn_with_containment` from the
`running-process-core` crate. That implementation stores the first
spawned child's PID as the group's PGID and then has every subsequent
child call `setpgid(0, first_child_pid)` from its `pre_exec` hook.

Once the first child exits and is reaped (e.g. the short
`avr-gcc -dumpversion` call emitted by `log_toolchain_version`), the
kernel tears down that process group. The second spawn's
`setpgid(0, stale_pgid)` then fails with EPERM, which surfaces as
`build error: build failed: io error: Operation not permitted (os
error 1)` immediately after the `Toolchain: avr-gcc 7.3.0` line —
exactly the failure reported in #129.

This is reproducible on Linux CI from 2.1.17 onwards (Build Leonardo
et al.) and blocks every AVR / ESP32 / etc. build made via the daemon.

Fix: bypass the shared-pgid behaviour on Unix. Install a per-child
`pre_exec` hook that creates a fresh process group with
`setpgid(0, 0)` and, on Linux, requests `PR_SET_PDEATHSIG(SIGKILL)` so
the kernel still kills the child when the spawning daemon thread
exits. Windows is unchanged — Job Object assignment is stateless and
has no analogous failure mode. macOS loses the drop-time `killpg`
backstop, which was already a no-op in practice because the global
`ContainedProcessGroup` lives in a `OnceLock` that never drops; this
is the same coverage profile as before the fix.

Regression test: `sequential_contained_spawns_do_not_fail_with_eperm`
in `crates/fbuild-core/src/containment.rs` initialises the global
group and performs two consecutive `spawn_contained` calls with a
wait+sleep between them, mirroring the AVR build's "dumpversion then
compile" shape.

Refs #129. Reproducing commits: #108 (containment feature) +
the interaction surfaced by #120 / #119 that made the second-spawn
path universally reachable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 19, 2026
The containment rewrite (#108) replaced `Command::output()` with a
hand-rolled `read_to_end(stdout)` → `read_to_end(stderr)` → `wait()`
sequence inside `fbuild_core::subprocess::run_command`. Reading the two
pipes serially deadlocks the moment the child fills either OS pipe
buffer (~64 KB): the child blocks on `write()` to the still-undrained
pipe, while the parent sits forever in `read_to_end()` on the other.

ESP32-C6 builds tripped this every run since 49307a3 — the riscv32-esp
GCC writes more than 64 KB to stderr on a full build, so every CI
invocation hung after `Toolchain: riscv32-esp-elf-gcc 14.2.0`, and the
client's 1800 s reqwest timeout was the only thing that ended the build
(`error: daemon error: stream error: error decoding response body`).

Fix:

- `run_no_timeout`: use `child.wait_with_output()`, which is the same
  background-thread drain that `Command::output()` used before.
- `run_with_timeout`: spawn two reader threads up front, keep the
  `try_wait` poll loop, join the readers after the child exits. Drains
  both pipes in parallel for the entire lifetime of the subprocess.

Both paths still spawn through `containment::spawn_contained`, so
process-tree containment from #108 is preserved.

Also adds `ci/find_direct_subprocess.py` — enumerates every remaining
direct `Command::new(...)` site in `crates/` so they can be migrated to
`running-process` and the wrapper-vs-direct split can be eliminated.
Tracked by #141; the script self-deletes when the count reaches zero.

Verified locally: `uv run cargo check`, `uv run cargo clippy
-- -D warnings`, all 5 fbuild-core subprocess unit tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 19, 2026
The containment rewrite (#108) replaced `Command::output()` with a
hand-rolled `read_to_end(stdout)` → `read_to_end(stderr)` → `wait()`
sequence inside `fbuild_core::subprocess::run_command`. Reading the two
pipes serially deadlocks the moment the child fills either OS pipe
buffer (~64 KB): the child blocks on `write()` to the still-undrained
pipe, while the parent sits forever in `read_to_end()` on the other.

ESP32-C6 builds tripped this every run since 49307a3 — the riscv32-esp
GCC writes more than 64 KB to stderr on a full build, so every CI
invocation hung after `Toolchain: riscv32-esp-elf-gcc 14.2.0`, and the
client's 1800 s reqwest timeout was the only thing that ended the build
(`error: daemon error: stream error: error decoding response body`).

Fix:

- `run_no_timeout`: use `child.wait_with_output()`, which is the same
  background-thread drain that `Command::output()` used before.
- `run_with_timeout`: spawn two reader threads up front, keep the
  `try_wait` poll loop, join the readers after the child exits. Drains
  both pipes in parallel for the entire lifetime of the subprocess.

Both paths still spawn through `containment::spawn_contained`, so
process-tree containment from #108 is preserved.

Also adds `ci/find_direct_subprocess.py` — enumerates every remaining
direct `Command::new(...)` site in `crates/` so they can be migrated to
`running-process` and the wrapper-vs-direct split can be eliminated.
Tracked by #141; the script self-deletes when the count reaches zero.

Verified locally: `uv run cargo check`, `uv run cargo clippy
-- -D warnings`, all 5 fbuild-core subprocess unit tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 24, 2026
…200)

Closes #141. Every fbuild subprocess now flows through
`fbuild_core::subprocess::{run_command,run_command_passthrough}`,
backed by `running_process_core::NativeProcess`, which drains stdout
and stderr concurrently from the moment the child starts. This
eliminates the class of pipe-buffer deadlocks that broke ESP32-C6 CI
after the hand-rolled drain in the containment rewrite (#108).

Migrated sites:
- fbuild-build: compiler_version, script_runtime harness + find_python
- fbuild-cli: find_pio, run_pio_command (via run_command_passthrough),
  open_in_browser, kill_process, find_daemon_pids

Intentional hold-outs annotated with `// allow-direct-spawn: <reason>`:
- zccache bootstrap and daemon spawns from CLI / Python (must outlive
  parent)
- containment module's own regression tests
- integration-test drivers that spawn binaries under test
- tokio async streaming emulator handlers (QEMU, avr8js/node) where
  the blocking NativeProcess API is unsuitable
- tokio parallel async fan-out in the CLI (IWYU, clang-tidy)

`ci/find_direct_subprocess.py` is promoted from a migration tracker
to a CI-enforced linter (`--fail`), wired into the Ubuntu check
workflow and a dedicated `lint-subprocess.yml`. Future direct
`Command::new` spawns without an allow-marker break the build.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

Daemon teardown hardening: process containment + socket lingering + console events

1 participant