Skip to content

fix: reopen snapshot ref for no-op withChanges - #13820

Merged
sipsma merged 2 commits into
dagger:mainfrom
vito:fix-withchanges-empty-changeset
Aug 3, 2026
Merged

sipsma merged 2 commits into
dagger:mainfrom
vito:fix-withchanges-empty-changeset

Conversation

@vito

@vito vito commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Problem

Once an empty changeset is applied anywhere on an engine, the engine can enter a state where every use of the parent directory — from any session — fails with invalid immutable ref 0x...: invalid until the engine restarts. In practice the poisoned parent is often the shared scratch directory (Query.directory), since SDK generators build outputs as dag.directory().withChanges(cs) and workspace staging overlays apply changesets onto the workspace root, both frequently with empty changesets. First observed as dagger generate failing repeatedly, with even a bare dagger core directory entries failing on the same engine.

Root cause

Directory.WithChanges has a fast path for an empty changeset that stored the parent's snapshot ref instance on the derived directory (dir.Snapshot.setValue(currentSnapshot)), introduced in #13342. Every cached Directory value owns its snapshot handle and releases it in Directory.OnRelease when its dagql cache entry is collected. Because the derived result shared the parent's handle, collecting the derived entry — e.g. disk-pressure GC pruning its persisted edge after its creating session closed — released the handle out from under the parent's still-cached, still-owned entry. Every subsequent mount of the parent then failed the released check in immutableRef.Mount.

Instrumented runs showed the signature clearly: one real release of the shared handle from gc → Prune → removePersistedEdge → collect → Directory.OnRelease, followed by dozens of no-op re-releases of the same pointer as other empty-withChanges derivatives of the same parent were collected. Before #13342 even no-op merges went through SnapshotManager.Merge, which returns a fresh handle in all paths, so no sharing occurred.

Fix

The empty-changeset path now reopens its own handle via SnapshotManager().GetBySnapshotID(...), the same convention subdirectory, diff, and the container rootfs clone helper already follow. An audit of every other Snapshot.setValue site in core/ found no other instance-sharing violation.

Testing

New regression test TestChangeset/TestWithChangesEmptyChangesetKeepsParentSnapshot: two sessions against a nested dev engine — one holds the parent entry, the other applies an empty changeset and disconnects — then a prune loop re-mounts the parent. Verified both ways: it fails with invalid immutable ref on the unfixed code and passes with the fix. Also validated end to end on a dev engine: repeated dagger generate runs across sessions plus explicit prunes no longer reproduce the failure.

🤖 Generated with Claude Code

vito added 2 commits August 1, 2026 13:41
Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>
Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in dagger#13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to vito/dagger that referenced this pull request Aug 1, 2026
Record the withChanges empty-changeset snapshot fix in the unreleased
changelog so the next release notes mention the invalid-immutable-ref
failure mode it removes.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito requested a review from sipsma August 1, 2026 17:59
@vito
vito force-pushed the fix-withchanges-empty-changeset branch from 66b12e8 to 934a440 Compare August 1, 2026 18:17
@sipsma
sipsma merged commit b1c2c81 into dagger:main Aug 3, 2026
89 checks passed
vito added a commit that referenced this pull request Aug 4, 2026
* test: reproduce withChanges snapshot poisoning

Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>

* fix: reopen snapshot ref for no-op withChanges

Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in #13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>

---------

Signed-off-by: Alex Suraci <[email protected]>
marcosnils pushed a commit to marcosnils/dagger that referenced this pull request Aug 24, 2026
* test: reproduce withChanges snapshot poisoning

Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>

* fix: reopen snapshot ref for no-op withChanges

Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in dagger#13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>

---------

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit b1c2c81)
marcosnils pushed a commit to marcosnils/dagger that referenced this pull request Aug 24, 2026
* test: reproduce withChanges snapshot poisoning

Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>

* fix: reopen snapshot ref for no-op withChanges

Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in dagger#13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>

---------

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit b1c2c81)
marcosnils pushed a commit to marcosnils/dagger that referenced this pull request Aug 25, 2026
* test: reproduce withChanges snapshot poisoning

Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>

* fix: reopen snapshot ref for no-op withChanges

Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in dagger#13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>

---------

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit b1c2c81)
marcosnils pushed a commit that referenced this pull request Aug 25, 2026
* test: reproduce withChanges snapshot poisoning

Applying an empty changeset with Directory.withChanges stores the parent's
snapshot ref instance on the derived directory. When the derived cache entry
is later collected (session close plus cache prune), its OnRelease releases
the shared handle, and the parent's still-cached snapshot becomes unusable:
every later mount, from any session, fails with "invalid immutable ref"
until the engine restarts. Reproduce with two sessions against a nested
engine: one keeps the parent entry alive while the other applies an empty
changeset and disconnects, then a prune loop re-mounts the parent.

Signed-off-by: Alex Suraci <[email protected]>

* fix: reopen snapshot ref for no-op withChanges

Directory.withChanges with an empty changeset returned the parent's snapshot
ref instance as the derived directory's snapshot. Every cached Directory
owns its handle and releases it when its cache entry is collected, so the
shared instance let a collected withChanges result invalidate the parent's
still-cached snapshot engine-wide (introduced in #13342). Reopen the
snapshot by ID instead, matching how subdirectory and diff derive their
handles.

Signed-off-by: Alex Suraci <[email protected]>

---------

Signed-off-by: Alex Suraci <[email protected]>
(cherry picked from commit b1c2c81)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants