Skip to content

fix(daemon): upgrade zccache, flush on exit, and wait on a starting daemon (#1480) - #1482

Merged
zackees merged 1 commit into
mainfrom
fix/1480-zccache-upgrade-daemon-startup
Sep 26, 2026
Merged

zackees merged 1 commit into
mainfrom
fix/1480-zccache-upgrade-daemon-startup

Conversation

@zackees

@zackees zackees commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

Closes #1480. Closes #1462.

Summary

Three changes so a slow daemon start is no longer reported as a failed build (#1462), and zccache stops starting cold after every daemon restart.

1. Upgrade zccache to main 3f9d4e6

Repins zccache in all five crates that use it, from 126380b to 3f9d4e6 (current zccache main). The new pin includes forge-ci/zccache#1710, which fixes forge-ci/zccache#1652:

Other version constraints are unchanged:

  • running-process stays at =4.10.14 from crates.io, which is what zccache resolves.
  • The zccache version stays 1.14.14, so the cache root (zccache/v1.14.14/) does not change and upgrading does not force a cold cache.
  • The lockfile diff touches only zccache crates.

2. Flush the embedded backend when the daemon exits

The daemon ends with std::process::exit(0), and the backend lives in a OnceLock, so zccache's flush, shutdown and drop paths never ran. That is why metadata.bin was missing on every start. The daemon now calls flush() on the backend before exiting, capped at 4 s. A normal flush takes under 100 ms. The cap keeps it below fbuild daemon stop's 5 s graceful budget and below the 10 s a replacement daemon waits for the root-ownership lock.

3. Report starting on /health, and have clients wait for it

  • fbuild-daemon/src/startup.rs (new, StartupGate): right after bind, the daemon duplicates the listening socket. A minimal responder on the duplicate answers every request with 503 {"status":"starting","phase":"usb_overlay|root_ownership|compile_backend",...}. Just before axum::serve, the gate stops the responder and hands the original socket to the full router. Both handles share one accept queue, so a connection that arrives during the hand-over waits in the backlog instead of being refused.
  • fbuild-core/src/daemon_health.rs (new): probe, classify, ReadinessDeadline and wait_until_healthy, shared by the CLI and the PyO3 bindings. The wait gives up after the base budget (10 s) on an endpoint that never answers. While the daemon reports starting, the deadline keeps moving forward, up to a 120 s cap. It never moves earlier, so a daemon that exits during init still fails quickly.
  • fbuild-cli: the spawn-winner poll, the spawn-loser wait and the broker adoption poll all use wait_until_healthy. When the endpoint already reports starting, the CLI waits for that daemon instead of spawning a duplicate. After 5 s of starting, it prints one line on stderr naming the phase.
  • fbuild-python: the same change for Daemon.ensure_running and AsyncDaemon.ensure_running.

Mixed versions keep working:

  • An old client sees the 503 as "not healthy yet", which is what it saw before.
  • An old daemon never reports starting, so a new client behaves as it did before.

Validation

  • soldr cargo clippy --workspace --all-targets -- -D warnings: clean.
  • bash test: all passing.
  • RUSTDOCFLAGS="-D warnings" soldr cargo doc: clean.
  • The CI guard scripts pass: path baseline, platform boundary, USB VID/PID, workspace crates, direct subprocess, dylint allowlists.
  • New tests:
    • fbuild-core::daemon_health: 10 tests. These include a real HTTP server that answers starting for about 2 s; the wait succeeds even though the base budget is 300 ms. An unreachable port still gives up after the base budget.
    • fbuild-daemon::startup: 3 tests. The phase is reported on /health, every route answers starting, and after the hand-over the full router answers healthy on the same port.
  • End to end with the real fbuild-daemon and fbuild binaries:
    • /health answered 503 {"status":"starting","phase":"usb_overlay",...} 27 ms after launch and moved to 200 once startup finished.
    • fbuild build of an uno sketch on a cold daemon succeeded through the new wait path. The log shows daemon startup phase: root_ownership → compile_backend → daemon startup complete in 0.3s.
    • fbuild daemon stop logged metadata cache flushed to disk … metadata.bin and zccache backend flushed. The next cold start logged metadata cache loaded from disk where every earlier start had logged metadata cache file not found, starting empty.

Review follow-ups (second commit)

  • The exit flush cap is now 4 s instead of 30 s, for the reason given above.
  • The CLI now treats a daemon that is still starting when its wait ends as stuck. It reports an error naming the phase and the daemon log path, instead of respawning and queuing more 120 s waits. New test: wait_on_a_daemon_stuck_starting_ends_at_the_starting_budget.
  • probe and wait_until_healthy now take the per-request timeout as a parameter, so the PyO3 client keeps its previous 5 s /health timeout and the CLI keeps 2 s.

Known limits (not changed here)

  • SIGTERM on Unix still skips the flush. The daemon's shutdown only listens for Ctrl+C, the HTTP shutdown endpoint and the Windows console events. Handling SIGTERM through graceful shutdown would make the signal wait for in-flight builds, which changes kill semantics, so that belongs in its own change.
  • An early shutdown can still lose metadata.bin. zccache's flush saves the depgraph before metadata, and first waits for the startup depgraph load to finish. If the daemon shuts down while that load is still running, the 4 s cap can expire before metadata.bin is written. zccache's periodic snapshot (ci: full / windows / Check hits its 45-min timeout-minutes on routine runs #1710) remains the fallback. Saving metadata before that wait belongs in zccache.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 18 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: FastLED/fbuild/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 04624fd1-62cd-403a-b969-31041a2ba577

📥 Commits

Reviewing files that changed from the base of the PR and between bf1c0e2 and 5c94271.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (14)
  • crates/fbuild-build-arm/Cargo.toml
  • crates/fbuild-build-engine/Cargo.toml
  • crates/fbuild-build-esp/Cargo.toml
  • crates/fbuild-build-mcu/Cargo.toml
  • crates/fbuild-build/Cargo.toml
  • crates/fbuild-cli/src/daemon_client.rs
  • crates/fbuild-core/src/README.md
  • crates/fbuild-core/src/daemon_health.rs
  • crates/fbuild-core/src/lib.rs
  • crates/fbuild-daemon/src/README.md
  • crates/fbuild-daemon/src/lib.rs
  • crates/fbuild-daemon/src/main.rs
  • crates/fbuild-daemon/src/startup.rs
  • crates/fbuild-python/src/daemon.rs

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.

zackees added a commit that referenced this pull request Sep 26, 2026
…ck-starting daemon (#1480)

Review follow-ups on #1482:
- Exit flush timeout 30 s -> 4 s (a normal flush takes <100 ms). Stays
  under `fbuild daemon stop`'s 5 s graceful budget and the 10 s a
  replacement daemon waits for the root-ownership lock, so a slow flush
  never escalates a stop to a kill or fails a restart spawn.
- CLI: a daemon still `starting` when the wait ends is reported as stuck
  (with its phase and the daemon log path) instead of being respawned,
  which only queued more 120 s waits behind it.
- daemon_health::probe/wait_until_healthy take the per-request timeout,
  so the PyO3 client keeps its previous 5 s /health timeout.
…aemon (#1480)

- Repin zccache to main 3f9d4e6, which carries forge-ci/zccache#1710:
  embedded depgraph load after readiness (the silent 12-47 s bring-up in
  #1462), timed bring-up phases, and a periodic metadata.bin snapshot.
- Flush the embedded zccache backend before the daemon exits, bounded at
  4 s (a normal flush takes <100 ms): under `fbuild daemon stop`'s 5 s
  graceful budget and the 10 s a replacement daemon waits for the root
  lock. process::exit runs no destructors and the backend lives in a
  OnceLock, so metadata.bin was never written and every start was cold.
- Serve `503 {"status":"starting","phase":...}` on every route from bind
  until the full router takes over (fbuild-daemon::startup::StartupGate,
  a duplicate of the bound listener sharing its accept queue).
- Shared fbuild_core::daemon_health: probe/classify, a ReadinessDeadline
  that stretches while the daemon reports `starting` (cap 120 s), and
  wait_until_healthy. The CLI and PyO3 clients use it for every readiness
  wait and wait on a starting daemon instead of spawning a duplicate; the
  CLI reports a daemon still `starting` at the cap as stuck (with its
  phase) rather than respawning behind it.

Closes #1480. Closes #1462.
@zackees
zackees force-pushed the fix/1480-zccache-upgrade-daemon-startup branch from 245290b to 5c94271 Compare September 26, 2026 16:38
@zackees
zackees merged commit 07b00b0 into main Sep 26, 2026
17 checks passed
@zackees
zackees deleted the fix/1480-zccache-upgrade-daemon-startup branch September 26, 2026 16:55
zackees added a commit that referenced this pull request Sep 26, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
zackees added a commit that referenced this pull request Sep 26, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
zackees added a commit that referenced this pull request Sep 26, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Sep 27, 2026
zackees added a commit that referenced this pull request Sep 27, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
zackees added a commit that referenced this pull request Sep 27, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
zackees added a commit that referenced this pull request Sep 27, 2026
…lush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.
zackees added a commit that referenced this pull request Sep 27, 2026
…lush (#1486)

* fix(daemon): controlled exit on SIGTERM — gate new work, drain 5 s, flush

Before this, SIGTERM on Linux/macOS (`fbuild daemon kill`, the escalation
in `fbuild daemon stop`, docker stop, CI teardown) killed the daemon with
no zccache flush and stale pid/port files. Routing it through the graceful
HTTP drain would instead wait out every running build.

- New fbuild_daemon::shutdown:
  - refuse_new_operations_when_shutting_down: operation routes (build,
    deploy, monitor, install-deps, reset, test-emu) answer 503 with a failed
    OperationResponse once shutdown has started (SIGTERM or HTTP shutdown).
  - exit_on_terminate: on SIGTERM, refuse new work, wait up to 5 s for
    in-flight operations, flush zccache (4 s cap), clean up records, exit.
  - persist_and_clean_up: the pid/port/claim/status cleanup + bounded flush
    now shared by every clean exit.
- DaemonContext::active_operations: exact in-flight count kept by
  OperationGuard (the operation_in_progress bool is cleared by the first of
  two concurrent operations to finish), plus wait_for_operations(budget).
- fbuild_core::daemon_health: SHUTDOWN_DRAIN_BUDGET (5 s), EXIT_FLUSH_BUDGET
  (4 s), TERMINATE_EXIT_BUDGET (their sum).
- `fbuild daemon stop`: after a delivered graceful terminate, wait
  TERMINATE_EXIT_BUDGET + 1 s before the forced kill so it never lands
  mid-flush; a refused terminate keeps the 5 s wait.

Follow-up to #1480 / #1482.

* fix(daemon): move SIGTERM listener into the platform layer

The platform boundary forbids #[cfg(unix)] outside fbuild_core::platform.
Add fbuild_core::platform::process::daemon_terminate_signal() (tokio
SIGTERM on Linux/macOS, never resolves on Windows, whose termination
requests already arrive via register_daemon_shutdown_handler) and use it
from the daemon. Regenerate ci/platform_boundary_research.tsv for the
shifted windows/process.rs rows.

* fix(daemon): synchronize shutdown admission

* fix(daemon): keep termination policy behind platform facade

* fix(daemon): test shutdown admission and refresh boundary inventories

* fix(paths): keep executable memo identity behind platform boundary

* Refresh platform boundary inventory after rebase

* fix(daemon): keep SIGTERM process exit in binary entry point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment