Skip to content

fix(daemon): fork contained children from a small spawn helper on Linux - #2211

Merged
zackees merged 4 commits into
mainfrom
fix/spawn-helper-2209
Nov 28, 2026
Merged

zackees merged 4 commits into
mainfrom
fix/spawn-helper-2209

Conversation

@zackees

@zackees zackees commented Nov 28, 2026

Copy link
Copy Markdown
Member

Fixes #2209. Sub-issue of forge-ci/soldr#4309.

Problem

Every contained daemon spawn on Linux installed a pre_exec hook (setpgid + PR_SET_PDEATHSIG). Any pre_exec hook makes std fork the whole multi-threaded daemon instead of calling posix_spawn, and every one of those forks went through one owner thread (#1628).

Change

  • Spawn helper (crates/fbuild-core/src/platform/linux/spawn_helper{,_server,_wire}.rs):
    • On the first contained spawn, a dedicated long-lived thread posix_spawns /proc/self/exe (argv0 fbuild-daemon) with a hidden __fbuild-spawn-helper <owner-pid> argument. The socket is passed as the helper's stdin. Because that thread lives for the whole process, the helper's parent-death signal is tied to the process (the ESP32 local-library compiles SIGKILLed (exit -9) since 2.5.30 — not OOM; userland kill of compile children #1628 rule).
    • The helper sets PR_SET_PDEATHSIG(SIGKILL) on itself, then checks getppid() so it exits if the daemon already died. It also checks that pidfds work, then sends Ready.
    • Requests arrive over a SOCK_SEQPACKET socketpair and carry argv, the full environment, cwd, and the three stdio fds (via SCM_RIGHTS). The helper forks from its own small image. In the child it applies setpgid(0,0) and PDEATHSIG, with a getppid guard against the helper itself. It reaps children through pidfds and reports Spawned{pid}, Failed{errno} or Exited{status}.
    • If the daemon goes away (EOF), the helper SIGKILLs every child group and exits.
  • Same semantics on the daemon side: the new ContainedTokioChild (platform/contained_child.rs) keeps the tokio-child surface: pipes, id(), async wait, start_kill/kill, wait_with_output, and kill-on-drop. Children still lead their own process group, so terminate_tree timeouts behave as before. Exec errors keep their errno, so ETXTBSY retries still work.
  • Wiring: the run_command* family in subprocess.rs (and subprocess_report.rs) now states its stdio and env plan and uses spawn_tokio_contained_planned. fbuild_daemon::entry::run dispatches the helper argument first and calls enable_spawn_helper() after init_containment. The emulator callers still use spawn_tokio_contained.
  • Fallback: the existing in-process path (owner thread + pre_exec) is used unchanged when the helper is unavailable. That covers: helper not enabled (CLI, unit tests), startup failed, the helper died, a request too large for one message (EMSGSIZE), or a non-Linux host (stub HelperChild is uninhabited).
  • Owner thread: it now serves only that fallback, so it stays (documented in owner_thread.rs and the linux README.md). A later migration to the shared helper in kernal-api (soldr-daemon: rustc spawns serialize on fork() in a large daemon; add a small spawn-helper process forge-ci/soldr#4309) can replace this module.

Tests

  • fbuild-daemon it::process_containment::spawn_helper_runs_commands_with_timeout_and_kill (Linux), run through containment_harness helper-run. It covers capture with cwd, env overlay and exit code, inherited env, stdin piping, a missing program failing to spawn, a 1 s timeout stopping a sh -c 'sleep 60 & wait' tree, kill() giving SIGKILL status, and 16 concurrent runs. It asserts the helper actually spawned the children.
  • ...::spawn_helper_and_its_children_die_with_the_owner (Linux): SIGKILLs the owner and asserts the helper and its sleep child are both gone (a zombie counts as gone).
  • Unit tests for the wire codec, fd passing across the socketpair, and request env construction.

Local: soldr cargo test -p fbuild-daemon --test it spawn_helper passes (2/2). The full local gate (bash test --full, workspace dylint) was not run: the shared build host was at load ~90 and the soldr daemon kept dropping compiles. CI is the validation of record.

Every contained spawn installed a pre_exec hook (setpgid + PR_SET_PDEATHSIG),
which makes std fork the whole multi-threaded daemon instead of using
posix_spawn, and all those forks were serialized on one owner thread.

The daemon now starts a small helper process lazily (posix_spawn of its own
image with a hidden argument, from a thread that lives for the process). The
helper arms PR_SET_PDEATHSIG on itself with a getppid guard, receives spawn
requests (argv, full env, cwd, stdio fds via SCM_RIGHTS) over a SOCK_SEQPACKET
socketpair, forks from its own small address space, applies setpgid +
PDEATHSIG in the child, reaps children via pidfds, and reports pid and exit
status back. The daemon keeps its wait/kill/process-group semantics through
ContainedTokioChild; the run_command family uses it. When the helper is
unavailable the existing in-process path (owner thread) is used unchanged.

Fixes #2209
…lper (#2209)

Gate the helper integration tests on the host OS at runtime instead of
cfg(target_os), refresh the platform-boundary inventory, and keep
entry/mod.rs under the 1000-line cap.
@zackees
zackees added this pull request to the merge queue Nov 28, 2026
Merged via the queue into main with commit f0c7d56 Nov 28, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

fbuild-daemon: every contained spawn fork()s the large daemon on one owner thread (PDEATHSIG pre_exec defeats posix_spawn)

1 participant