Skip to content

fix(daemon): bound runtime teardown so a slow zccache exit flush cannot hold the cache-root writer lock #1990

Closed

Description

@zackees

Summary

A graceful shutdown of fbuild-daemon can leave the process alive for a minute or
two after it has logged daemon exiting. While it lingers it still holds
zccache's cache-root writer lock (~/.fbuild/prod/zccache/<ver>/.writer.lock),
so every replacement daemon on that root refuses to start:

WARN zccache::core::cache_root_lock: cache root already has a live writer; refusing to start a second one cache_root=/home/niteris/.fbuild/prod/zccache/v1.15.8 pid=<new daemon>
fatal: failed to start embedded zccache service: ... cache root ... unusable: another live daemon already holds this cache root as its writer
fbuild-daemon cannot start without an embedded zccache backend. Check .../zccache permissions and disk space.

The user-visible result is that fbuild deploy / fbuild build fail for
30-40 s with

daemon error: daemon did not become healthy after 3 spawn attempts (10s each).

and a hint about permissions and disk space that has nothing to do with the
real cause.

Mechanism

crates/fbuild-daemon/src/shutdown.rs::persist_and_clean_up bounds the zccache
exit flush with EXIT_FLUSH_BUDGET (4 s) and logs a timeout — but the flush
runs on a spawn_blocking thread and Tokio cannot cancel it. run() then
returns, which drops the runtime, and Runtime::drop waits for every
blocking task to finish. So the "bounded" flush is only bounded in the log; the
process keeps running (holding the writer lock) until that write finishes.

The SIGTERM path is not affected: it calls std::process::exit(0) right after
drain_and_persist_on_terminate, so the runtime is never dropped there.

Reproduction (Linux, this host, 2026-11-11)

ESP32-C6 on USB-JTAG (ser=8C:BF:EA:CF:87:B4), load average ~20:

  1. fbuild deploy . -e esp32c6 -p /dev/ttyACM1 --skip-build (success).
  2. The daemon self-evicts 30 s after idle; its log shows
    zccache backend flush on exit timed out after 4s then daemon exiting.
  3. ps -p <pid> still reports the daemon alive ~60 s later, and
    /proc/<pid>/fd still points at .writer.lock.
  4. Three immediate daemon spawns are refused with the live-writer error; a
    plain flock on the same file from another process fails while the corpse
    is alive and succeeds once it finally dies.

Proposed fix

Bound the runtime teardown in run() with
runtime.shutdown_timeout(RUNTIME_TEARDOWN_BUDGET) after
block_on(run_async(...)), so an abandoned flush cannot hold the lock past the
budget it was already given. Abandoned blocking tasks die with the process,
exactly as they do today on the SIGTERM path.

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

    bugpriority: p1

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions