Skip to content

fix(subprocess): stop a timed-out command's process tree before reporting failure - #1868

Merged
zackees merged 2 commits into
mainfrom
agent/1867-subprocess-tree-cleanup
Nov 3, 2026
Merged

zackees merged 2 commits into
mainfrom
agent/1867-subprocess-tree-cleanup

Conversation

@zackees

@zackees zackees commented Nov 3, 2026

Copy link
Copy Markdown
Member

Problem

run_command's timeout path returned FbuildError::Timeout as soon as the
deadline expired. Dropping the Tokio child stops that child only
(kill_on_drop), so everything the command had forked kept running — and kept
writing the artifact the caller had already been told had failed.

Reported in #1867 from an ESP32-S3 link that hit the fixed 300 s deadline in
Esp32Linker::link: cc -> collect2 -> ld stayed alive after
build error: timeout: command timed out after 300s, held firmware.elf open
at 0 bytes, and only later grew it to 15,177,316 bytes. A retry into that
output directory therefore raced a live writer.

Fix

Give every timed-out invocation a cleanup boundary that includes its
descendants, and finish that cleanup before the failure is returned:

  • platform::process::terminate_tree(pid, termination) — new facade next to
    terminate_pid. Linux/macOS signal the process group the child leads
    (setpgid(0, 0) is already applied by the contained spawn path), and signal
    it only when the kernel confirms pid leads the group, so an uncontained
    child that shares fbuild's own group can never take fbuild down. A tree that
    is already gone is a successful no-op, matching the containment contract for
    a finished command.
  • subprocess::wait_and_capture now drains the streams alongside child.wait()
    and keeps the handle after the deadline, so on timeout it can stop the tree
    (terminate_tree) and reap the direct child before returning Timeout.
    run_command_passthrough gets the same boundary.
  • No global process-name kills, no daemon restart, no client-side retry, and no
    raised/disabled deadline: the 300 s budget and every configured timeout stay
    as they are — the guarantee added is that nothing the command owned outlives
    the error.

Tests (RED -> GREEN, hardware-free)

crates/fbuild-core/src/subprocess_timeout_tests.rs (registered from
subprocess.rs) drives the production run_command /
run_command_passthrough paths with sh -c fork/detached-writer shapes:

  • timed_out_capture_command_stops_descendant_writers
  • timed_out_passthrough_command_stops_descendant_writers
  • terminate_tree_of_a_missing_pid_reports_success
  • a_completed_command_still_returns_its_captured_output (>64 KiB stdout +
    stderr, guarding the drain rewrite)

RED (before the fix): both writer tests failed with
a descendant of the timed-out command survived and wrote ".../late-write.txt".
GREEN (after): 3 passed (and 4 with the capture guard), fbuild-core lib
suite 365 passed; 0 failed; 2 ignored.

Scope / honesty

  • The reported failure is Linux, and Linux + macOS are fixed.
  • Windows is not covered by this PR. Tree kill there needs the Job Object
    handle that the contained async spawn path does not hand back; terminate_tree
    therefore degrades to the single-process kill on Windows, and the existing
    KILL_ON_JOB_CLOSE containment is unchanged. Tracked as the Windows half of
    fix(subprocess): linker timeout returns failure while descendant ld keeps writing firmware.elf #1867.
  • A configurable ESP32 link deadline is not added here; the issue asks that it
    never substitute for cleanup, so it belongs in its own change.

Closes #1867.

A deadline is not a completion. Dropping the Tokio child stopped only the
direct child, so a compiler driver's descendants (cc -> collect2 -> ld) kept
running and kept writing the artifact the caller had been told had failed
(#1867).

- platform::process::terminate_tree signals the process group the child leads,
  and only when the kernel confirms that pid leads it: an uncontained child
  shares fbuild's own group and must never take fbuild down. A tree that is
  already gone is a successful no-op.
- wait_and_capture drains the streams alongside child.wait() and keeps the
  handle past the deadline, so on timeout the tree is stopped and the child
  reaped before Timeout is returned; run_command_passthrough gets the same
  boundary.
- Configured timeouts are untouched. The guarantee added is that nothing the
  command owned outlives the error.

Windows still degrades to the single-pid kill (the contained async spawn path
does not retain the Job Object handle it creates); that remains the Windows
half of #1867.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

The macOS terminate_tree backend adds three libc:: host-mechanic rows
(getpgid/kill), so the deterministic inventory must be rewritten. Linux is
unaffected: its backend uses hand-declared externs, not libc::.

Verified: --check --host-label linux rc0, --host-label windows rc0, and
enforce_platform_boundary.py --print-totals 44/43 rc0.
@zackees
zackees added this pull request to the merge queue Nov 3, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 3, 2026
Merged via the queue into main with commit a842f9e Nov 3, 2026
30 checks passed
@zackees
zackees deleted the agent/1867-subprocess-tree-cleanup branch November 3, 2026 03:26
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.

fix(subprocess): linker timeout returns failure while descendant ld keeps writing firmware.elf

1 participant