GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — engine/snapshots: one ancestry lease for dagql owners by vito · Pull Request #15232 · dagger/dagger

Conversation

@vito

@vito vito commented Nov 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Snapshot owner leases are gc.flat: the collector keeps what a lease names and not its parents, so AttachLease named a snapshot's whole ancestor chain, snapshots and blobs, in every dagql owner lease. Lease records then grow with owners × chain depth. A chain of N results, each a layer on the last (withNewFile, withExec, ...), costs N²/2 records, and since the containerd metadata store is the in-memory fastbolt, all of them stay on the heap.

Design

  • A dagql owner lease (dagql/result/<id>/...) names only its own snapshot and that snapshot's blobs.
  • Each snapshot manager has one shared flat ancestry lease, dagql/result/owner-ancestry/<random generation>, which names every ancestor of an owned snapshot once. The manager keeps reference counts in memory. A snapshot is kept while some owner lease owns it or while a kept child has it as its parent. The ancestry lease names exactly the kept snapshots that have kept children, plus their blobs.
  • Attach walks Stat from the snapshot up to the first ancestor that is already kept. It writes the owner lease and the newly kept ancestors in one metadata transaction.
  • Remove deletes the owner lease first. It then releases the counts down the chain and takes out of the ancestry lease whatever drops to zero. If that write fails, the release is retried on the next removal.
  • Locking: ownerLeaseLocker → ancestryMu → cm.mu. Every change to the ancestry runs under ancestryMu: attach, remove, and recording a blob for an ancestor the ancestry lease names.
  • Restart: at boot, dagql attaches every owner lease it keeps again, which rebuilds the ancestry under a new generation. DeleteStaleDaggerOwnerLeases then deletes older generations' ancestry leases. It keeps them instead if any kept owner lease has not been attached.
  • The ancestry lease uses the dagql/result/ prefix on purpose. On a downgrade, an engine that predates it deletes it at boot as a stale owner lease, after attaching full chains to the owners it keeps. It is not leaked forever.
  • Other AttachLease callers keep naming full chains: pins, operation leases, services and scratch. They can be deleted directly from the lease manager.
  • Known limit: owner leases written by an older engine still name their full chains after an upgrade, until their results are re-created. This is no regression; the savings apply to new owners.

Numbers

TestOwnerLeaseRecordsOn* (in-memory records and live heap after GC):

before after
200 results on a 200-deep chain 20,100 lease resources, +4.5 MB (22.6 KB/result) 399 resources, +393 KB (2.0 KB/result)
200 results on one 50-deep snapshot 10,000 resources, +2.2 MB 249 resources, +264 KB

Attaching 2,000 owner leases in parallel, as the boot re-attach does (chain depth 1 / 50 / 200): 26 / 116 / 323 ms on main, 32 / 33 / 36 ms here.

Dev engine, a 52-deep withExec chain with three results on its tip: 107 owner leases hold 110 resources, and the ancestry lease holds 54. Naming each chain in each owner lease would take an estimated ~1,400 resources.

hack/memory-soak (on memory-fixes-combined, 10 sessions, HeapAlloc after GC): no measurable difference. The last session ended at 203.4 MB before and 202.8 MB after. Snapshot and metadata heap is about 5 MB in both. The soak's chains are shallow, and its results are pruned between sessions. This change targets deep chains of retained results.

Testing

  • engine/snapshots (also with -race), with new tests:
    • restart and re-attach;
    • a kept owner that is not attached keeps the old ancestry lease;
    • concurrent attach and remove at random depths, checked against GC;
    • ancestry write failure, and retry of a failed release;
    • a blob recorded for an ancestor during an attach or a removal, and a blob shared by two ancestors (the two race tests fail without the fix);
    • pins mixed with dagql owners.
  • Unit tests in ./core, ./dagql, ./core/schema and ./engine/engineutil/....
  • Integration tests against a dev engine:
    • TestDirectory/TestLongEditChain, TestContainer/TestLongExecChain and the WithNewFile tests;
    • the TestLocalCache GC and prune tests (8);
    • TestCachePersistence/TestDiskPersistenceAcrossRestart (11 subtests, including local cache survival, native snapshotter and unclean shutdown).
  • Manual: local-cache prune with no session empties the ancestry lease. A prune while a session holds a 50-deep chain leaves the chain evaluating correctly.

Measured

One session builds 16 parallel chains of 1000 withNewFile edits, each result synced. Main (fcce0f8) → all memory PRs combined, with the session still open: retained heap under leases and the bolt metadata store 250 → 55 MiB (4.6×).

Chart and all demos: memory cake. Reproduce: memoryCake.demo15232(ref: "memory-cake-all") from #15237 (workload in fixtures/15232/).

Confidence and impact

Impact: Medium. Lease records for retained deep chains drop from owners × depth to owners + depth (200-deep chain: 4.5 MB → 393 KB of heap, 20,100 → 399 records), but the shallow memory-soak workload showed no measurable change (~203 MB both).

Confidence: Medium. Unit, race, GC/prune and restart-persistence integration tests pass, and two rounds of review findings are fixed with regression tests. A downgrade, an upgrade of a long-lived production cache, and a manual chain-survives-restart check (the lab engine prunes everything at shutdown) were not exercised.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vito added 7 commits November 18, 2026 10:25
@vito
engine/snapshots: measure owner lease records
5fad236
Owner leases cost memory proportional to results times chain depth:
AttachLease records every ancestor of the snapshot for the lease, as a
containerd lease resource (a node of the in-memory fastbolt metadata store)
and as two in-memory owner records. One result per snapshot of a 200-deep
chain, as a chain of Directory.withNewFile calls makes, holds 20100 lease
resources and 40200 owner records, +4.5 MB of live heap (22.6 KB per result,
~224 B per pair); 200 results on one 50-deep snapshot hold 10000 (+2.2 MB).

The tests log these numbers, and can write a heap profile with
-owner-lease-heapprofile.

Signed-off-by: Alex Suraci <[email protected]>
@vito
engine/snapshots: one ancestry lease for owners
a27e721
Owner leases are gc.flat, so AttachLease named a snapshot's whole ancestor
chain, snapshots and blobs, in every dagql owner lease. That costs
O(owners x chain depth) lease resources, and with the in-memory fastbolt
metadata store all of it is retained heap.

A dagql owner lease ("dagql/result/") now names only its tip snapshot and
that snapshot's blobs. Each manager has one shared flat lease,
dagger/owner-ancestry/<random generation>, that names every ancestor of an
owned tip. Refcounts are kept in memory: a snapshot is kept while some owner
has it as a tip or a kept child has it as a parent. AttachLease walks Stat
from the tip up to the first ancestor already kept, then writes the owner
lease and the newly kept ancestors in one transaction. RemoveLease deletes
the owner lease first, then decrements the refcounts down the chain and
removes ancestry resources that drop to zero. Lock order is ownerLeaseLocker,
then ancestryMu, then cm.mu.

recordSnapshotContent also attaches blobs to the ancestry lease for
snapshots it names, and PersistentMetadataRows counts ancestry members as
owned. DeleteStaleDaggerOwnerLeases deletes the ancestry leases of other
generations last, after dagql has re-attached every kept owner at boot.
Other AttachLease callers (pins, operation leases, services, scratch) still
name the full chain.

TestOwnerLeaseRecordsOn*, one result per snapshot of a chain:

  200-deep chain:  20100 -> 399 lease resources,
                   +4.5 MB -> +381 KB live heap (22.6 KB -> 1.9 KB/result)
  200 results on one 50-deep snapshot:
                   10000 -> 249 lease resources, +2.2 MB -> +264 KB

Signed-off-by: Alex Suraci <[email protected]>
@vito
engine/snapshots: ancestry lease as dagql/result/
8c24a6b
Name the owner ancestry lease dagql/result/owner-ancestry/<gen>. An engine
that predates the ancestry lease deletes every dagql/result/ lease it does
not keep at boot, after attaching the whole chains of the owners it keeps to
those owners again: on a downgrade it frees the ancestry lease instead of
leaking every snapshot and blob it names for good.

TestSnapshotTransferTypedAdoptionAndRestart counts leases and owners; the
ancestry lease, which holds the imported snapshot's ancestors, is neither,
so the test leaves it out.

Signed-off-by: Alex Suraci <[email protected]>
@vito
engine/snapshots: hold ancestryMu to record blobs
05a7ae9
recordSnapshotContent checked under cm.mu whether the ancestry lease named a
snapshot, dropped the lock to attach the blob, and counted it afterwards.
A RemoveLease releasing the snapshot in that window either left the blob
attached to the ancestry lease with nothing counting it, so it was never
deleted, or, for a blob a released ancestor shared, deleted it from the
lease before it was counted for the snapshot that stays kept: the collector
then took the blob of an owned ancestor.

The ancestry part of a blob record now runs under ancestryMu, like every
other change to the ancestry, from the check to the count. Records for
snapshots the ancestry lease does not name skip it: an attach that names
the snapshot later picks the blob up in attachPendingAncestryBlobs.

The two race tests fail without the change. A third covers the attach
picking up a blob recorded during it, which no test reached.

Signed-off-by: Alex Suraci <[email protected]>
@vito
engine/snapshots: harden ancestry lease cleanup
34ce080
Two cleanup paths could lose track of what the ancestry lease holds:

- RemoveLease drops the in-memory counts before it deletes the released
  ancestors from the ancestry lease. When that write failed, a retry found
  nothing to release, and the ancestors stayed until a restart. What a
  failed write leaves is now kept, under ancestryMu, and taken out by the
  next removal, minus anything the ancestry names again by then.
- DeleteStaleDaggerOwnerLeases deletes the previous process's ancestry
  lease, which holds the ancestors of every owner it kept, assuming each
  kept owner lease has been attached again. dagql attaches them all first;
  should a kept one not be attached, the old ancestry lease now stays for a
  later start, rather than letting its owner's ancestors be collected.

Signed-off-by: Alex Suraci <[email protected]>
@vito
dagql: note owner lease attaches share ancestry
5f0afa8
The boot re-attach of owner leases runs on all cores, but dagql owner leases
now share the snapshot manager's ancestry lease, whose changes the manager
serializes. Each attach walks to its first kept ancestor, so the serialized
part is about one snapshot and one metadata write: 2000 parallel attaches on
a 200-deep chain take 36ms, against 323ms when each lease named its chain.

Signed-off-by: Alex Suraci <[email protected]>
@vito
engine/snapshots: document ancestry locking
b708a40
The ancestry's blobs change only under ancestryMu and mu, like its counts;
dagql owner-lease attaches run one at a time end to end; and the pruning
doc names the ancestry lease as what keeps a result's ancestors.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 19, 2026
@vito
memory-cake: add #15232 demo
a0ff956
One session builds 16 withNewFile chains 1000 results deep, every result
synced so it owns its snapshot. On main each owner lease names its whole
chain (up to 128 layers): focus 235.8 MiB live, 54.1 MiB on the PR branch,
63.7 MiB on memory-cake-all. ~20-30 s per session.

Signed-off-by: Alex Suraci <[email protected]>
tiborvass
tiborvass approved these changes Nov 19, 2026
View reviewed changes
@vito
vito marked this pull request as draft November 20, 2026 16:00
@vito

vito commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

🤖 I'm parking this as a draft after a second-look review. The code looks correct, but on a real engine it saves very little, and its stated reason for the change turned out to be wrong.

  • Small reward on a real engine. On a live dev engine running many agent sessions, lease and bolt metadata was 182 MiB of a 15.5 GiB heap. Of that, the owners × depth term this PR removes is at most ~60 MiB (≤0.4%). Overlay chains flatten at 128 layers (flatten.go:23), so depth is capped at 127 in production, and the 200-deep unit test overstates the effect.
  • The premise is off. gc.flat doesn't stop containerd from keeping a leased snapshot's parents: core/metadata/gc.go:622-642 in containerd v2.2.5 sends the parent for flat snapshots too, and only label refs (blobs) are skipped. containerd's own gc_test.go:420-421 expects this, and a probe test here confirms it. fix(engine): lease parent layer content for tarball export #13307 added the chain walk to keep ancestor blobs for export, so the ancestry lease here really protects blobs. The comments at manager.go:112 and cache_usage.go:217 are still correct. The new comment in owner_ancestry.go and the cache_pruning.md edit are not.
  • Cost: about 550 production lines, a new engine-wide lock, lease generations, and a partial-commit leak on one failure path. It also conflicts with engine: keep contenthash trees on disk #15213 in persistentMetadataRows, and engine: keep contenthash trees on disk #15213 is the much bigger win (~2.2 GiB live), so that one goes first.

A question for @sipsma, who may know the history: why are owner leases gc.flat? A non-flat owner lease that names only the tip would let containerd follow both parents and the gc.ref.content.blob labels. That could replace both the chain walk from #13307 and this PR's bookkeeping. The one caveat I can see is manifest/config content recorded in memory against image-top snapshots, which has no label.

If this is revived, it needs:

  1. Fixed lint: gofmt in manager.go:164, gocyclo on attachOwnerLease, staticcheck in owner_lease_memory_test.go:98.
  2. A corrected rationale in the comments, the docs and this description.
  3. wcprof.Lock for ancestryMu.
  4. A fix for the partial-commit leak on the dagql attach path.
  5. A guard test that checkpoint rows still include ancestry members, so a rebase over engine: keep contenthash trees on disk #15213 can't silently drop them.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sipsma

sipsma commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

A question for @sipsma, who may know the history: why are owner leases gc.flat? A non-flat owner lease that names only the tip would let containerd follow both parents and the gc.ref.content.blob labels. That could replace both the chain walk from #13307 and this PR's bookkeeping. The one caveat I can see is manifest/config content recorded in memory against image-top snapshots, which has no label.

If this is revived, it needs:

  1. Fixed lint: gofmt in manager.go:164, gocyclo on attachOwnerLease, staticcheck in owner_lease_memory_test.go:98.
  2. A corrected rationale in the comments, the docs and this description.
  3. wcprof.Lock for ancestryMu.
  4. A fix for the partial-commit leak on the dagql attach path.
  5. A guard test that checkpoint rows still include ancestry members, so a rebase over engine: keep contenthash trees on disk #15213 can't silently drop them.

@vito it's pretty much vestigial at this point and just survived the buildkit->dagql transition

When caching moved from buildkit to dagql in #11856, we kept it on purpose: every engine lease flat, and our own metadata, not containerd labels, deciding which blobs belong to a snapshot. It was never about parents. You're right that flat snapshots still follow them, and the design notes from then say so. #14264 later put the gc.ref.content.blob label on snapshots but left the leases flat, and we never revisited that. I agree a non-flat owner lease on the tip should be able to replace the #13307 chain walk and this PR's bookkeeping. Besides the manifest/config caveat, which a label on the image-top snapshot would cover, the checkpoint's content-hash rows and the transfer pins lean on the walk today and would need to follow.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vito

vito commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

🤖 Closing for now, thanks @sipsma for the history. We'll come back to this if lease bookkeeping turns out to matter. On a live dev engine the lease slice was 182 MiB of a 15.5 GiB heap, and this PR would save at most about 60 MiB of it.

What we'd come back to: your direction, not this PR. Instead of the gc.flat owner leases, each owner gets a lease that names only its tip snapshot and lets containerd follow parents and gc.ref.content.blob labels. That would delete both the #13307 chain walk and this PR's ancestry bookkeeping. It needs:

  • a label on image-top snapshots for manifest/config content;
  • the checkpoint's content-hash rows (persistentMetadataRows) and the transfer pins, moved off the chain walk.

Why not now: the risk is out of proportion to the saving.

  • Getting GC wrong loses data: ancestor blobs go missing on export, which is exactly the failure fix(engine): lease parent layer content for tarball export #13307 fixed.
  • Restart behaviour is subtle. Checkpoint rows that silently drop ancestry members change digests after a restart.
  • Existing caches keep their old flat leases until results are re-created, so both models have to coexist for a while.
  • Proper coverage needs restart and export tests with blob-backed chains, which we don't have yet.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vito vito closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tiborvass tiborvass tiborvass approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vito @sipsma @tiborvass
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.