Skip to content

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

Closed

Description

@zackees

Context

An ESP32-S3 link reached fbuild's fixed 300-second deadline and returned a terminal build failure, but a descendant ld process continued running and writing firmware.elf afterward. The project cannot safely retry into that output directory until the abandoned process finishes: the previous failed build still owns an output writer after fbuild has reported completion.

Observed during FastLED/FastLED#5001 hardware validation on Linux with fbuild 2.5.49+git.98affea702ff (98affea702ff58e551124cda68f863659acebb7c). The run used the stock daemon in a private DEV/cache configuration. The host was heavily loaded, so the deadline itself is not evidence of incorrect linker behavior or a compiler hang.

The affected downstream workflow was an AutoResearch run equivalent to:

bash autoresearch esp32s3_opi_psram8 --lcd --worker-core 1 \
  --upload-port /dev/ttyACM1 --lanes 1 --strip-sizes 700 \
  --frames 4 --timeout 3m --skip-lint

The AutoResearch timeout above governs monitoring; it does not change the linker subprocess deadline. The build/deploy phase ended with:

Running: .../.venv/bin/fbuild .../.build/fbuild/esp32s3_opi_psram8 deploy -e esp32s3_opi_psram8 -p /dev/ttyACM1
build error: timeout: command timed out after 300s
Deploy failed (fbuild) [860.4s]
BUILD+FLASH FAIL

The total duration includes compilation before linking. The live process tree observed for the link was:

compiler driver PID 204500
  collect2 PID 204579
    ld PID 204609

After the terminal timeout, ld PID 204609 remained alive. A later observation showed about 76 seconds of CPU time over 6m13s of wall time, consistent with a slow, scheduled linker rather than proof of a wedge. The ELF was observed at zero bytes while the orphan still held it, then grew to 15,177,316 bytes when that process eventually exited. No second linker was started against the same output until the previous writer was gone. This investigation did not kill processes, operate the board, or modify source.

Confirmed source path

  • Esp32Linker::link passes an explicit Some(Duration::from_secs(300)) to run_command.
  • wait_and_capture consumes the Tokio child in wait_with_output(). When tokio::time::timeout expires, it returns FbuildError::Timeout without terminating and reaping the descendant tree before returning.
  • spawn_tokio_contained supplies direct-child kill-on-drop and daemon owner-death containment. These are not a per-command descendant cleanup guarantee while the daemon remains alive.
  • The Linux containment setup creates a process group and sets a parent-death signal on the launched child, but the timeout path does not explicitly terminate/reap that command's group. The compiler driver's own descendants can therefore outlive the dropped direct child.
  • FBUILD_SUBPROCESS_DEFAULT_TIMEOUT_SECS affects calls without an explicit deadline. The ESP32 link's explicit 300-second value wins, so changing that default is not an existing way to extend this link deadline.

Proposal

Give each owned subprocess invocation a cleanup boundary that includes its descendants. On timeout or cancellation, stop and reap that invocation's process tree, drain/close its output handling, and finish cleanup before releasing the project/build lock or sending the terminal failure response. Use the repository's process abstraction so Linux process groups and Windows containment follow the same ownership contract. Never terminate the daemon's entire compiler group or other clients' work.

Keep a bounded linker deadline. A separately configurable, validated ESP32 link deadline could accommodate slow/LTO-heavy hosts, but it must not substitute for cleanup: every configured timeout must leave no writers behind. Write failed outputs to a temporary path or otherwise invalidate them only after all owned writers have stopped; a late ELF must not become an apparently successful artifact.

Acceptance criteria

  • RED -> GREEN: first add a focused hardware-free test using the production run_command path. Its launcher must spawn a grandchild that keeps an output file/pipe open and attempts a delayed write after the timeout. Demonstrate the current escaped-descendant failure before implementing the fix.
  • After the fixed command returns timeout, every process owned by that command has exited and been reaped within a bounded cleanup budget, and no delayed write or further ELF mutation occurs.
  • Cover timeout and cancellation, inherited stdout/stderr handles, and the Linux/Windows process-tree backends. Keep normal successful commands working.
  • An unrelated concurrent command/build remains alive. A retry for the failed project's output directory begins only after the previous command's writers are gone, and cannot consume a partial or late-published ELF.
  • Re-run an ESP32-S3 compiler-driver -> collect2 -> ld case under load with an intentionally short test deadline. Record the process tree before/after and stable output state after the terminal result.
  • If a configurable link deadline is added, test its validation and precedence explicitly; keep default behavior bounded and document that the generic default-timeout environment variable does not override explicit call-site budgets.

Decisions

  • The bug is abandoned process-tree/output ownership after timeout, not the fact that a heavily loaded host exceeded five minutes.
  • Cleanup is per invocation and must finish before lock/response completion. No global process-name kills, daemon restarts, or client-side sleeps/retries are an acceptable fix.
  • No hardware access is needed for the primary regression; the ESP32 link is end-to-end confirmation after the host process contract is fixed.

Related issues

Activity

  1. zackees commented on Nov 3, 2026

    @zackees
    MemberAuthor

    Opened #1868 for the process-tree ownership half of this report:

    • platform::process::terminate_tree stops the process group a contained child
      leads, and only when the kernel confirms that pid leads it — an uncontained
      child that shares fbuild's own group can never take fbuild down.
    • wait_and_capture (captured) and run_command_passthrough now stop the tree
      and reap it before FbuildError::Timeout is returned, so a retry cannot
      begin while an abandoned ld still owns firmware.elf.
    • Configured deadlines are untouched (the 300 s ESP32 link budget stays).

    RED -> GREEN is a hardware-free sh -c fork/detached-writer test on the
    production run_command path (both writers survived before the change).

    Two items from this issue are deliberately not in #1868:

    1. Windows tree kill — the contained async spawn path does not hand back the
      Job Object handle it creates, so terminate_tree degrades to the
      single-process kill there. Needs the handle handoff.
    2. A separately configurable ESP32 link deadline — it must not substitute for
      cleanup, so it belongs in its own change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions