Skip to content

Fix failed to commit on during finalize error - #6821

Merged
tonistiigi merged 3 commits into
moby:masterfrom
tonistiigi:fix-failed-to-commit
Jun 8, 2026
Merged

tonistiigi merged 3 commits into
moby:masterfrom
tonistiigi:fix-failed-to-commit

Conversation

@tonistiigi

Copy link
Copy Markdown
Member

replaces #6808

Updated fix. Changes:

  • Removed the complicated TestExecFailedOutputDoubleOwnerFinalizeRepro as it heavily relies on stubs
  • Added similar case for File next to Exec.

I couldn't get a reasonable integration test but manual POC and analysis is in https://gist.github.com/tonistiigi/fb61083d56f8d70a952f9993a9334d8d that fails for both exec and file case before the patch is fixed by this.

Some other similar fixes in #6820

@cpuguy83

cpuguy83 and others added 2 commits June 1, 2026 20:53
When an ExecOp exec fails, ExecOp.Exec commits each output mutable
snapshot to an immutable ref, appends it to the results returned to the
solver, AND stores it (via the error-decoration defer) into the
ExecError. The error path stored the SAME *workerRefResult into
ExecError.Mounts that was already handed to the solver, so a single
committed output ref was owned through two independent channels but
backed by only one counted cache ref.

When the error owner is released (e.g. gateway/solver error cleanup),
that single release drains the shared refcount to zero, cascading into
equalMutable.release and deleting the mutable record's lease. The active
snapshot is protected only by that lease, so a subsequent containerd
metadata GC collects it. The surviving solver-side owner is then used as
the upper parent of a Diff, whose Finalize -> Snapshotter.Commit runs on
the already-collected active snapshot, producing:

  failed to finalize upper parent during diff: failed to commit <active>
  to <final> during finalize: failed to stat active key during commit:
  snapshot <active> does not exist: not found

The fix is twofold:

  - exec.go: clone the result (res.Clone()) before embedding it in
    ExecError.Mounts so the error owner has its own counted ref.
  - worker/result.go: make workerRefResult.Clone produce an independent
    *WorkerRef (and thus an independent cache ref) instead of sharing the
    embedded one and orphaning the original.

Both are required: the call-site clone is what stops the over-release,
and the Clone-method fix is what makes that clone actually independent
(otherwise it aliases the shared *WorkerRef and leaks the original ref).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
The layerParent and diffParents cases locked each parent's mutex and
deferred the unlock to the end of the function, so the lock was held
longer than the release it protects -- and in the diffParents case the
lower parent's lock was still held while the upper parent was locked and
released.

Unlock each parent immediately after releasing it so the lock is held
only for that parent's release, matching the mergeParents case in the
same switch which already does this. This narrows the lock scope and
avoids holding one parent's lock while acquiring another's.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
cpuguy83
cpuguy83 previously approved these changes Jun 2, 2026
@cpuguy83

cpuguy83 commented Jun 2, 2026

Copy link
Copy Markdown
Member

Oh except CI is failing.

@cpuguy83
cpuguy83 dismissed their stale review June 2, 2026 16:42

CI failing

FileOpSolver.getInput wrapped each input cache.ImmutableRef in a fresh
*workerRefResult without cloning, so ExecError.Inputs shared the same
*immutableRef entry with the caller. A release on either side cascaded
into equalMutable.release on a ref the other side still owned, surfacing
as "snapshot does not exist" on a later Finalize -- same shape as the
prior failed-exec output ref fix.

Clone the ref so ExecError owns its own counted ref.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
@tonistiigi
tonistiigi merged commit fdbf563 into moby:master Jun 8, 2026
360 of 365 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants