Any prune that removes a record makes the next build uncached when the base image was pulled by the engine (Prune calls ReleaseUnreferenced with a canceled context)
Bug description
When the base image of a build was pulled by the engine (docker pull with the containerd image store), any docker buildx prune / docker builder prune that removes at least one record, even an unrelated one, makes the next build of that Dockerfile fully uncached.
- The records the build used before are still on disk and still listed by
docker buildx du, but they are never matched again. They stay as orphans until something deletes them. - Expected: records the prune did not remove keep being used as cache.
This reproduces on Docker 29.1.3 (BuildKit v0.26.2) and on Docker 29.8.1 (BuildKit v0.33.0). BuildKit master still has the same code.
We found it on a small production host (Docker 29.1.3, docker driver, containerd image store). A multi-stage Node.js Dockerfile, whose base node:20-slim had been pulled with docker pull, was built around 20 times. Cache share per build, from docker buildx history inspect ("Build Steps"):
| Builds | Cached steps | What ran before the build |
|---|---|---|
| 12 builds, Sep 20–24 | 42% (31% when package.json changed) |
no prune that removed records |
| Sep 24 14:16 | 0% | docker builder prune -f --filter until=24h. The base, apt-get and node_modules records that every build reuses survived it. |
| 5 builds, Sep 24 15:58–17:44 | 42% | docker builder prune --max-used-space 10GB, which removed nothing |
| Sep 24 18:32 | 0% | the same --max-used-space prune, this time removing 31 old, unrelated records |
| 5 later builds | 0% each | the same prune after every build, each time removing records |
Reproduction
This uses an isolated engine, so no local cache is touched:
docker run -d --rm --privileged --name bk-repro docker:29.1.3-dind # same result with docker:dind (29.8.1)
docker exec -i bk-repro sh -eu <<'EOF'
until docker info >/dev/null 2>&1; do sleep 1; done
mkdir -p /r && cd /r
printf 'FROM alpine:3.20\nRUN echo a > /a\nRUN echo b > /b\n' > Dockerfile
printf 'FROM scratch\nCOPY unrelated.txt /\n' > Dockerfile.u
echo x > unrelated.txt
docker pull -q alpine:3.20 # omit this line for the control run
docker buildx build -o type=cacheonly . # builds everything
docker buildx build -o type=cacheonly . # both RUN steps CACHED
docker buildx build -o type=cacheonly -f Dockerfile.u . # one unrelated record
docker buildx prune -f --filter 'description~=unrelated' # removes only that record (8.192kB)
docker buildx build --progress=plain -o type=cacheonly . 2>&1 | grep -E '(RUN|CACHED|DONE)'
docker buildx history inspect "$(docker buildx history ls --format '{{.Ref}}' | head -1 | sed 's|.*/||')" | grep 'Build Steps'
EOF
docker stop bk-reproResults:
| Engine / BuildKit | Base image | 2nd build (before prune) | Build after the prune | echo a records in buildx du |
|---|---|---|---|---|
| 29.1.3 / v0.26.2 | docker pull first |
6/6 (33% cached), both RUN CACHED |
6/6 (0% cached), both RUN re-executed | 2 (old one orphaned) |
| 29.1.3 / v0.26.2 | pulled by BuildKit (control) | 6/6 (33% cached) | 6/6 (33% cached), both RUN CACHED |
1 |
| 29.8.1 / v0.33.0 | docker pull first |
6/6 (33% cached) | 6/6 (0% cached), both RUN re-executed | 2 |
| 29.8.1 / v0.33.0 | pulled by BuildKit (control) | 6/6 (33% cached) | 6/6 (33% cached) | 1 |
The prune output was the same in every run: a single 8.192kB record removed (Total: 8.192kB).
Root cause (analysis)
- In
Controller.Prune,ctxcomes fromerrgroup.WithContext(stream.Context())(L219).eg.Wait()runs insideeg2(L257), anderrgroupcancelsctxas soon asWaitreturns. The deferredc.ReleaseUnreferenced(ctx)(L231) runs only whendidPruneis true, so it always gets a canceled context. cacheManager.ReleaseUnreferencedcallsc.results.Exists(ctx, id)for every result and releases the result when that returns false.cacheResultStorage.Existsreturns false on any error fromload, not only when the record is missing.loadgoes throughWorker.LoadRef,cacheManager.GetandgetRecord, which runscheckLazyProviders. That callsisLazy(ctx), which callsContentStore.Info(ctx, blob)for every ancestor still flagged blob-only. With the canceled context that call fails,Existsreturns false, and the result is released.- Records of a base image that the engine already unpacked stay blob-only forever.
GetByBlobcreates them withblobOnly=true(L302). Thenunlazyreturns early because the snapshot already exists, sounlazyLayernever clears the flag (L1413). That is why the control run, where BuildKit pulls and extracts the base itself, is not affected.
The automatic GC path does not have this problem. gc() triggers throttledReleaseUnreferenced, which calls releaseUnreferencedCache(context.TODO()) (L133). Master still has the same Prune code (control/control.go L248 and L260 at f6b5e08).
Related, but a different problem:
- #7175 is about lock contention in
ReleaseUnreferenced. - #7177 (open) switches this call to
stream.Context()as part of an on-demand release endpoint. That would probably avoid this bug, but it is motivated by #7175, and the impact described here is not tracked anywhere.
Possible fixes:
- In
Prune, passcontext.WithoutCancel(ctx)(orstream.Context()) toReleaseUnreferenced. - Also, or instead, make
cacheResultStorage.Existsreturn true unless the load error is a not-found error, so that a transient or context error never releases cache results.
Workaround for users: avoid partial prunes. After a prune that removed anything, the remaining records are not reused anyway, so prune everything (prune -af) when disk space is needed, and otherwise leave the cache alone. Automatic GC is not affected.
Side note, probably a separate issue: my first repro attempt used FROM alpine:3.20 + RUN for the unrelated record, and the prune removed nothing because buildx du reported that record as shared. A record that was never exported has an empty blob, so its layerDigestChain() is the base chain plus an empty digest. layerKey skips empty digests, so the image ref checker matches the record to alpine:3.20 in the image store. Such records are also left out of --max-used-space accounting, since only non-shared records count.
Version information
Production host where it was found:
OS: Ubuntu 24.04.5 LTS, aarch64 (Ampere A1 VM), kernel 6.17.0-1020-oracle
Docker Engine: 29.1.3 (Ubuntu package docker.io 29.1.3-0ubuntu3~24.04.2)
Buildx: 0.30.1 (docker-buildx 0.30.1-0ubuntu1~24.04.1)
BuildKit: v0.26.2 (vendored in Docker 29.1.3), builder "default", driver "docker"
Storage: overlayfs, driver-type io.containerd.snapshotter.v1 (containerd image store)
containerd: 2.2.1, runc 1.3.4Reproduction (Docker Desktop on macOS arm64, isolated docker:dind):
docker:29.1.3-dind engine 29.1.3, buildx v0.30.1, BuildKit v0.26.2, overlayfs (io.containerd.snapshotter.v1)
docker:dind engine 29.8.1, buildx v0.37.1, BuildKit v0.33.0, overlayfs (io.containerd.snapshotter.v1)Source: moby/buildkit