0045. Rust worker runtime image and graceful shutdown
Source:
docs/decisions/0045-rust-worker-runtime-and-shutdown.md. Generated at build time; edit the source file.
Date: 2026-11-21. Status: accepted.
Context
Section titled “Context”R1 Task 22 turns the Rust crates into the deployable worker. The worker must survive a deploy (SIGTERM) without losing or burning work, refuse to analyse with the wrong engine, and ship as a small, locked-down image.
Decision
Section titled “Decision”- One process, one runner per job kind: import (concurrency 2, no hard timeout: the handler
owns its 30-minute idle deadline), analyse (concurrency = engine pool size, hard timeout 11 min
over the handler’s own 10-minute deadline) and reconcile (concurrency 1, hard timeout 5 min).
The pool size is
WORKER_POOL_SIZE(passed through compose;.env.examplesets 8 for a desktop), elsemin(cores - 2, 8)and at least 1, so a backfill cannot saturate the machine. The database pool haspool_size + 8connections (budget inworker/src/start.rs), and every connection asks the server for TCP keepalives (60/10/3 s). - Boot order: check that
STOCKFISH_PATHis Stockfish 19.x (thesf19-n300k-v1protocol is pinned to it; anything else is a typedStartErrorand exit 1), adopt a Drizzle database and migrate it, connect, start the engines and the runners, schedule the reconcile. A failed step closes what the earlier ones created. - Shutdown (25 s grace; compose
stop_grace_period: 30s): stop claiming, drain for 60 % of the grace, then close the engine pool (in-flight analyses put their game back topendingand release their job without using an attempt, even on the final attempt), drain until 90 %, then force the runners: the 90 %-100 % window bounds the wait for their handlers (cancel, then abort, even a handler that ignores cancellation), and thenrelease_ownedalways runs to completion, bounded only by the queue’s query timeout and the hard deadline below, not by the window. (Amended 2026-11-22: the release used to get only what was left of the window, at least 50 ms. On a loaded host that write took longer, was dropped, and left the job leased for a whole lease and charged a stall.) Then close the engines and the database pool. Every step is bounded by the remaining budget (at least 100 ms); exit code 1 only if a close failed or timed out. That 100 ms floor lets the steps overrun the grace slightly, so the whole shutdown also has a hard deadline atgrace + hard_after(25 s + 3 s = 28 s, before the 30 s SIGKILL): whatever is still running is dropped, the code is 1 and the process exits; jobs not yet released are reclaimed when their lease expires. SIGTERM and SIGINT start it once; later signals are logged and ignored. - Config is safe to log:
WorkerEnvhas a hand-writtenDebugthat shows the database URL with its password replaced by***and the Lichess token only asSome("***"). - Batched enqueue:
Queue::enqueue_manyinserts many jobs of one kind in one statement with the same columns and live-only dedupe rule asenqueue, under the queue’s statement timeout, with one NOTIFY per batch. The reconciler uses it with the analyse job’s own options. on_deadgeneration fence: a dead-lettered analyse job does not mark its game failed while the game isanalysingwith a heartbeat younger than three heartbeat periods (30 s): that is a live run, possibly of a newer generation. A stalled run is always older than that (lease plus stall grace, 85 s).- Import runs are status-fenced:
finishandfailonly touch a run that is stillrunning, so a run the reconciler closed for a stale heartbeat stays closed. - Runtime image (
backend/Dockerfiletargetworker): cargo-chef planner and builder stages, thendebian:bookworm-slimpinned by digest withca-certificates, the checksum-pinned Stockfish 19 and theworkerandclibinaries;USER 10001, no ports, noCONTACT_EMAIL(ADR 0043). Logs are JSON lines on stdout with"service":"worker";LOG_LEVEL=silentturns them off, otherwiseRUST_LOGfilters.
Consequences
Section titled “Consequences”- A deploy never burns a job attempt: a job interrupted by shutdown is released and re-run by
the next worker. It does still count against the game’s
games.analysis_attempts, which every claim increments (the attempt fence, ADR 0035): an interrupted run is one of the game’sMAX_ANALYSIS_ATTEMPTS(6) after which the reconciler fails it. Frequent restarts mid-analysis can therefore exhaust a game; deferred to T23/T24. - A hard-timed-out analysis whose handler never winds down leaves its game
analysing; the reconciler’s stale sweep (10 min) resets or fails it. - Writes of an import are still not routed through its lock connection: if that connection dies mid-import, a re-claimed job can overlap the old handler (T21 limitation, deferred to T23/T24).
- Bumping the Debian base means updating the digest by hand.