Skip to content

fix(buildkitdrive): never rewrite a live daemon's record on a slow or cancelled probe - #685

Merged
github-actions[bot] merged 1 commit into
mainfrom
fix/buildkit-ensure-live-record
Dec 2, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
fix/buildkit-ensure-live-record

Conversation

@aitrawog

@aitrawog aitrawog commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

buildkitdrive.Ensure treated any failed readiness probe as "no daemon is listening". Two things make that probe fail:

  • a busy host, where buildctl debug workers misses the 500 ms budget (readinessInterval*5);
  • a caller whose context is already done.

In either case it rewrote the shared buildkitd.toml of a live daemon with the caller's options, then tried to start a second daemon on the same root. deploykit.EmitVmBox passes DaemonOptions{}, which renders no max-parallelism key. So the record read 0 while the daemon kept running at 4. From then on every build on the host failed with a false mismatch, until someone killed the daemon by hand:

buildkitdrive: the running daemon was configured with max-parallelism=0 but 4 was requested; ... stop the daemon

This is the state the shared daemon on the operator's host was in on 2026-12-02: the record was rewritten at 16:16:36 local, the daemon log was untouched, and the daemon had been alive since 2026-12-01 23:09. It also very likely explains #665's measured "idle daemon at 0".

The fix, in Ensure:

  • A done context ends the call without touching anything. It writes no record and starts no daemon.
  • Liveness comes from the daemon's own one-instance lock on the state root (buildkitd.lock), not from probe latency. A live daemon that did not answer in time is waited for and attached to. Its record is never rewritten, and a second daemon is never started on its root.
  • A drive-level ensure.lock (spec/lock) serializes probe → decide → write → start → ready across processes. Concurrent callers on an empty root no longer each spawn a daemon and leave the last writer's record.
  • attach() and waitReady() are extracted. Both the "listening" and the "live but slow" paths use the same compare and wait, so neither is a copy.

Closes #684.

Files

File Change
buildkitdrive/daemon.go Ensure: ensure lock, done-ctx early return, daemonOwnsRoot liveness, extracted attach() / waitReady()
buildkitdrive/paths.go EnsureLockName = "ensure.lock", documented beside LockName
buildkitdrive/ensure_live_test.go four regression tests: a slow probe on a live daemon, a done ctx on a live daemon, a done ctx with no daemon, and 8 concurrent callers on an empty root

How tested

# head f54f752583ba219f5c0aa1c8b10fd94289bae922 (git rev-parse HEAD)
$ git diff --stat origin/main...HEAD
 buildkitdrive/daemon.go           | 131 +++++++++++++++++++-------
 buildkitdrive/ensure_live_test.go | 193 ++++++++++++++++++++++++++++++++++++++
 buildkitdrive/paths.go            |   5 +
 3 files changed, 295 insertions(+), 34 deletions(-)
$ gofmt -l .
$ go vet ./...                                          # exit=0
$ go test ./...                                         # exit=0, 21 packages ok
$ go test -count=3 -race -run 'TestEnsure' ./buildkitdrive
ok  	github.com/opencharly/sdk/buildkitdrive	1.943s
$ GOOS=linux GOARCH={amd64,arm64,arm,386} CGO_ENABLED=0 go build ./...   # exit=0 each
$ GOTOOLCHAIN=go1.26.8 golangci-lint run ./...          # v2.13.2, the CI pin
0 issues.

golangci-lint runs under GOTOOLCHAIN=go1.26.8, go.mod's floor, because the host's Go 1.27 cannot typecheck under the v2.13.2 binary (opencharly/opencharly#1400).

The tests fail without the change. The new tests ran against origin/main's daemon.go and paths.go, with only the EnsureLockName constant added so the package compiles:

--- FAIL: TestEnsureSlowProbeOnALiveDaemonKeepsItsRecord
    ensure_live_test.go:90: a second daemon was started on a live root (Started=true, spawns=1)
    ensure_live_test.go:92: the live daemon's record was rewritten:
--- FAIL: TestEnsureDoneContextOnALiveDaemonTouchesNothing
    ensure_live_test.go:108: Ensure with a done context must return it, got <nil>
--- FAIL: TestEnsureDoneContextWithNoDaemonWritesNoRecord
    ensure_live_test.go:131: a record was written for a daemon that was never started (stat err=<nil>)
--- FAIL: TestEnsureConcurrentCallersStartOneDaemon
    ensure_live_test.go:191: 3 daemons were spawned on one root by concurrent callers, want 1

The changed path, run live on a real rootless daemon (rootlesskit + buildkitd), with a 30-line program that calls buildkitdrive.Ensure. The daemon is private (CHARLY_CACHE_HOME/CHARLY_RUNTIME_DIR relocated), so the shared host daemon was not touched.

Before, with sdk v0.2026334.1628 (the version charly 2026.335.1816 embeds):

1) start private daemon at 4:                        OK started: true
   toml: max-parallelism = 4 | log lines=9 | rootlesskit procs=2
2) live daemon, CANCELLED ctx, DaemonOptions{}:      ERR: buildkitdrive: start rootlesskit: context canceled
   toml: <no max-parallelism key> | log lines=9 | rootlesskit procs=2
3) a build asking for 4:                             ERR: ... configured with max-parallelism=0 but 4 was requested ...
4) live daemon (record restored), ONE probe > 500 ms, DaemonOptions{}:   OK started: true
   toml: <no key> | log: [rootlesskit:parent] error: failed to lock .../rootlesskit-state/lock, another RootlessKit is running with the same state directory?

After, with this head (go mod edit -replace onto the worktree, built-against-f54f752):

1) start private daemon at 4 (fixed sdk):            OK started: true
   toml: max-parallelism = 4 | log lines=25 | rootlesskit procs=2
2) live daemon, CANCELLED ctx, DaemonOptions{}:      ERR: context canceled
   toml: max-parallelism = 4 | log lines=25 | rootlesskit procs=2
3) live daemon, ONE probe slower than 500 ms, DaemonOptions{}:   OK started: false
   toml: max-parallelism = 4 | log lines=25 | rootlesskit procs=2
4) a build asking for 4:                             OK started: false
   toml: max-parallelism = 4 | log lines=25 | rootlesskit procs=2

Spike

The RCA's candidate fixes, each judged against the two reproduced signatures:

  • (a) Liveness from the daemon's lock, no write on a done ctx, plus an ensure lock. This is the winner. The liveness signal was spiked first, on the private daemon:

    daemon running:  .../buildkitd/buildkitd.lock: HELD (daemon owns it)
    daemon stopped:  .../buildkitd/buildkitd.lock: NOT held
    

    It then cleared both signatures live (the "After" block above) and passed all four regression tests.

  • (b) A longer probe deadline. Rejected. Any finite deadline can be exceeded on a loaded host, so the conflation of "slow" with "absent" remains. Signature 2 (a done ctx) is unaffected by any deadline. This is the C5 band-aid shape.

  • (c) Compare the request against the running daemon's own --config file (found through the lock holder's cmdline). Not chosen for this change. It would make attach truthful after a poisoning, but the record would still be rewritten and duplicate daemons still spawned: it treats the symptom of the race, not the race. Proposed on #1390 by vol-cdi; it remains open for discussion on buildkitdrive.Ensure overwrites a LIVE daemon's record on a slow or cancelled probe — every later build then fails with a false 'max-parallelism=0' mismatch #684.

Rulebook compliance

Plan conformance

Winning candidate: (a).

Planned change DESIGN requirement Disposition
liveness via spec/lock.AcquireFileLock (non-blocking) on buildkitd.lock; ensure.lock via the same primitive D-SDK-1 / §4 step 4 conforms — "every shared mechanism — an executor, the store, a lock, a readiness wait, a matcher — comes from sdk, never a private copy (D-SDK-1, D-CODE-3)." The lock is the shared spec/lock primitive (which kit/filelock.go re-exports), and waitReady is the one readiness wait, now shared by both paths.
ensure.lock held only for the ensure sequence, released on return or process death D-STORE-7 conforms — "locks die with their holder." flock is released by the kernel when the holder exits.
the daemon stays the one builder; only its ensure logic changes §14.3 build conforms — "One builder, BuildKit, builds every image."
Ensure made safe for concurrent callers through a host-resource lock D-PROTO-9 conforms — "Plugin methods are safe to call concurrently for different identities." Concurrent Ensure calls now start one daemon and never rewrite a live one's record (TestEnsureConcurrentCallersStartOneDaemon). The sentence's second half, "Locking is core's alone (D-DEP-4)", is the DEPLOYMENT lock, which this does not take. A per-uid host resource guarded through the shared spec/lock primitive is an existing row of that mechanism, used on the same path: plugin-build/candy/plugin-build/buildkit.go:143 kit.AcquireImageBuildLock, plugin-vm/candy/plugin-vm/vm_build.go:102, sdk/deploykit/data.go:172.

Change classification

  • Change class: library (sdk).
  • Gate: gofmt, go vet ./..., go test ./..., -race on the Ensure tests, the cross-compile matrix, and golangci-lint v2.13.2, all on the committed head.
  • Tier: fully tested and validated.
  • Authoring runtime: Claude Code on model ID claude-opus-5-5 (Anthropic Claude Opus 5.5).
  • Breaking change + rollback: none. Ensure's signature and its success results are unchanged. A live-but-slow daemon is now attached to (it used to be raced), and a done ctx now returns ctx.Err(), where it used to succeed or fail opaquely. Roll back by reverting this squash commit.

Filed issues

https://claude.ai/code/session_01Tvdn3t2LD2HiQqQM8sxByy

Agent: openclaw-cdi · Claude Code session 7011c83d-8b59-4309-bde8-b5d3bb83fd75
Assisted-by: Claude Code Anthropic Claude Opus 5.5 (fully tested and validated)

… cancelled probe

Ensure treated ANY failed readiness probe as "no daemon": a probe that
missed its 500 ms deadline on a busy host, or a caller whose context was
already done. It then rewrote the shared buildkitd.toml with the caller's
options and tried to start a second daemon. DaemonOptions{} (EmitVmBox)
renders no max-parallelism key, so the record said 0 while the daemon ran
at 4, and every later build failed with a false "configured with
max-parallelism=0 but 4 was requested" mismatch until a manual kill.

- A done context ends Ensure without touching the record or starting a
  daemon.
- Liveness is decided by the daemon's own one-instance lock on the state
  root (buildkitd.lock): a live daemon that did not answer in time is waited
  for and attached to, never rewritten and never doubled.
- A drive-level ensure.lock (spec/lock) serializes probe, write, start and
  ready across processes, closing the start race in which concurrent
  callers each spawned a daemon and the record was the last writer's.

Four regression tests fail on the previous tree and pass here.

Closes #684

Claude-Session: https://claude.ai/code/session_01Tvdn3t2LD2HiQqQM8sxByy
Agent: openclaw-cdi · Claude Code session 7011c83d-8b59-4309-bde8-b5d3bb83fd75
Assisted-by: Claude Code Anthropic Claude Opus 5.5 (fully tested and validated)
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Review — PASS

Head SHA: f54f752583ba219f5c0aa1c8b10fd94289bae922 (branch: fix/buildkit-ensure-live-record)

Security screen: PASS — no secrets, no egress, no weakened guardrails, no supply-chain change (no go.mod/go.sum edit; spec/lock is the existing in-org primitive). Placement: CORRECT (existing buildkitdrive mechanism in sdk; no core/alias growth, no capability pulled inward).
Checklist: All applicable rules PASS or NA.

Non-blocking notes (do not affect the verdict):

  • The ### Spike block instruments only candidate (a); (b) and (c) are rejected on reasoned grounds (C5-forbidden band-aid / out-of-cause symptom treatment) rather than pasted runs. Acceptable, but naming them "candidate fixes" invites the C2 "every candidate spiked" reading — reframing them as non-candidates would be cleaner.
  • opencharly/opencharly#1400 is cited inline as a pre-existing environment issue but is not repeated under ## Filed issues; listing it there would be tidier.
  • The acknowledged zero-opinion START case (same false-mismatch symptom, different trigger) is deferred to buildkitdrive: an idle daemon with a different max-parallelism blocks every build until manually killed #665; it is separable, listed, and not introduced here — filed, not blocking.

Verdict: PASS


ollama-cloud/deepseek-v4.1-flash — action-review.

View action run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant