Cancelling a build mid-RUN leaks the step's cache refs until the daemon restarts (`errResults` on a released result proxy)
Description
When a build is cancelled while a RUN step is executing, the step's layer
refs are recorded in the solver's error-result list but never released. They
stay "in use" for the life of the process: docker builder prune (and
BuildKit's own GC) skips them, docker system df counts them under build
cache as in-use, and only restarting the daemon frees them. On a CI runner
where cancellation is routine (Woodpecker's cancel-previous on every push
while a pipeline is running) this grows without bound until the disk fills.
Where
solver/llbsolver/result.go. The result proxy's Result() path appends the
ExecError's outputs to rp.errResults and returns on the cancelled path
before re-checking rp.released, so a proxy that was already released has
new refs appended after its Release ran. Release does not clear
errResults, and the finalizer that would otherwise catch a leaked ref is
disarmed because the refs are handed over with OwnerBorrowed.
Introduced in df3a9cad2 ("make sure execerror is released on cancellation", 2020-11-25), i.e. present from v0.8.0 through v0.33.0 (Docker 29.8.1).
Reproduction (Docker 29.8.1 / BuildKit v0.33.0, overlay2, one runner)
cat > Dockerfile <<'D'
FROM alpine
RUN dd if=/dev/urandom of=/big bs=1M count=512 && sleep 60
D
docker builder prune -af
docker build --no-cache . & # wait until the RUN is executing
sleep 15; kill -INT %1; wait # cancel mid-RUN (CI cancel does the same)
docker system df -v | grep -A3 'Build cache' # record is "in use", not reclaimable
docker builder prune -af # reclaims 0B
systemctl restart docker
docker builder prune -af # now reclaims itEach cancelled build adds another pinned record. In our case ~20 GB of cache was in-use with no build running, on a 59 GB runner disk.
Related
moby/buildkit#5583 (closed 2025-01 for lack of reproduction) describes the same symptom.
Suggested fix
Re-check rp.released after the cancelled-path append (and release the
appended refs if the proxy is already released), and have Release drain
errResults. A patch can follow if the maintainers want one.
Source: moby/buildkit