Repository navigation
fix(daemon): fork contained children from a small spawn helper on Linux - #2211
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2209. Sub-issue of forge-ci/soldr#4309.
Problem
Every contained daemon spawn on Linux installed a
pre_exechook (setpgid+PR_SET_PDEATHSIG). Anypre_exechook makes std fork the whole multi-threaded daemon instead of callingposix_spawn, and every one of those forks went through one owner thread (#1628).Change
crates/fbuild-core/src/platform/linux/spawn_helper{,_server,_wire}.rs):posix_spawns/proc/self/exe(argv0fbuild-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).PR_SET_PDEATHSIG(SIGKILL)on itself, then checksgetppid()so it exits if the daemon already died. It also checks that pidfds work, then sendsReady.SOCK_SEQPACKETsocketpair and carry argv, the full environment, cwd, and the three stdio fds (viaSCM_RIGHTS). The helper forks from its own small image. In the child it appliessetpgid(0,0)andPDEATHSIG, with a getppid guard against the helper itself. It reaps children through pidfds and reportsSpawned{pid},Failed{errno}orExited{status}.ContainedTokioChild(platform/contained_child.rs) keeps the tokio-child surface: pipes,id(), asyncwait,start_kill/kill,wait_with_output, and kill-on-drop. Children still lead their own process group, soterminate_treetimeouts behave as before. Exec errors keep their errno, so ETXTBSY retries still work.run_command*family insubprocess.rs(andsubprocess_report.rs) now states its stdio and env plan and usesspawn_tokio_contained_planned.fbuild_daemon::entry::rundispatches the helper argument first and callsenable_spawn_helper()afterinit_containment. The emulator callers still usespawn_tokio_contained.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 (stubHelperChildis uninhabited).owner_thread.rsand the linuxREADME.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-daemonit::process_containment::spawn_helper_runs_commands_with_timeout_and_kill(Linux), run throughcontainment_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 ash -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 itssleepchild are both gone (a zombie counts as gone).Local:
soldr cargo test -p fbuild-daemon --test it spawn_helperpasses (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.