Repository navigation
dagql: share the egraph lock on cache-hit lookups, session tracking and ID loads - #14892
Merged
Merged
Conversation
grouville
self-requested a review
October 28, 2026 02:34
grouville
approved these changes
Oct 28, 2026
…nd ID loads Every dagql call took the cache's global egraphMu exclusively, several times: cache lookup, session result tracking, publication, handoff release, dependency edges and ID loads. The work under the lock is small, but with hundreds of concurrent calls the lock becomes a hand-over queue. Building dagger's CLI with a per-package Go build, the engine's goroutines waited 63-68 s in total on mutexes during a first build, and 29-38 s during an edit rebuild, most of it on this lock. The per-call paths that only read the e-graph now hold it for reading: - lookupCacheForRequest answers a miss, or a recipe-digest hit that teaches nothing, under the read lock, and claims the hit for the session in the same section. A hit that must teach the e-graph its identity (extra digests, TTL, persistable, or a structural route) changes nothing under the read lock and repeats the lookup under the write lock, as before. - trackSessionResult and sharedResultByResultID (ID loads, including the canonical-equivalent search) hold the read lock. What those sections write has its own synchronization: the session edge is recorded under sessionMu, last-used stamps under the result's payloadMu, and the incoming ownership count is now atomic. Every decrement, and every check that drives collection, still holds the write lock, so no claim can run concurrently with them. The read paths use the existing non-mutating eq-class root lookup instead of the path-compressing find. Signed-off-by: Erik Sipsma <[email protected]>
The cache model treated every critical section as one action. With the lookup hit, session tracking and ID-load claims now holding egraphMu for reading, read sections can overlap each other. Modeling each as one action is sound only if they commute; this makes the model check that. A new constant SharedSections selects how they are modeled. "atomic" keeps every existing configuration exactly as before. "split" models the lookup hit and the ID-load claim each as a selection step and a claim step, with other read sections, release marking and lock-free steps free to run between them and every write-locked step excluded until the claim (WriteLockAvailable). "split_stale_edge" is a deliberate mutation, the claim trusting the session edge it read at selection, which must violate OwnershipExact. New configurations: shared_claims (two sessions' overlapping hits and a release), shared_claims_prune (two sessions' overlapping hits with prune and persistable requests), shared_claims_resources (resources_latedep_recheck split, with ID loads on an imported graph) and the mutation shared_claims_stale_edge. Signed-off-by: Erik Sipsma <[email protected]>
Release marking and its sessionMu snapshot do not take egraphMu, so they can run while a read section is open; only release cleanup holds the write lock. The claim's tombstone check orders the claim against them. Signed-off-by: Erik Sipsma <[email protected]>
…ounds shared_claims_prune had not finished after 100 minutes at ten TLC workers (270 million distinct states, the queue still growing). It now lets only s1 release, with call-only symmetry, and passes with about 309 million. It keeps three invocations, so two lookup hits still overlap: s1's release marking and snapshot can interleave with s2's open read section, while cleanup and prune wait until every read section has closed. s2's release interleavings with prune are no longer explored there; shared_claims covers both sessions releasing, without prune. The stale-edge mutation is what shows a configuration can observe two overlapping read sections, so it has to run at that configuration's exact bounds. shared_claims_stale_edge had nested calls on while shared_claims has them off; it now matches shared_claims exactly. The new shared_claims_prune_stale_edge does the same for shared_claims_prune. Both violate OwnershipExact within seconds, with two lookup hits selected and unclaimed at once. Signed-off-by: Erik Sipsma <[email protected]>
sipsma
force-pushed
the
sipsma/dagql-egraph-shared-lock
branch
from
October 28, 2026 02:38
8322a88 to
a963b62
Compare
This was referenced Oct 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What this changes for users
Rebuilding after an edit is faster. Building dagger's CLI with a per-package Go build, an edit rebuild was about 0.4–0.5 s faster (4–5%). A first build didn't change measurably, and an unchanged rebuild was 20 ms slower (0.6%).
These were measured on an engine built from main as of 27 Oct with #14866 and #14872 applied, not on this branch's base. Since then, #14806 stopped caching reflected primitive getters, which changes how often this lock is taken. So the effect on current main hasn't been measured.
Why
Almost every dagql call takes the cache's global lock,
egraphMu, exclusively, several times: cache lookup, session result tracking, publication, handoff release, dependency edges and ID loads. The work done under the lock is small. But with hundreds of concurrent calls, goroutines queue on it, and handing the lock from one to the next costs more than the work.What changes
The per-call paths that only read the e-graph now hold
egraphMufor reading:trackSessionResult) holds the read lock.sharedResultByResultID), including the canonical-equivalent search, hold the read lock.What those sections write has its own synchronization:
sessionMu;payloadMu;Every decrement, and every check that drives collection, still takes the write lock, so no claim can run concurrently with them. The read paths find e-graph roots with the existing non-mutating lookup, because the usual find compresses paths, which is a write.
Measurements
dagger's CLI with dagger.io/go's per-package build, on the earlier engine described above (main as of 27 Oct with #14866 and #14872), with and without this change. One 16-core AWS machine (c8gd.4xlarge), a fresh engine per run, 3 interleaved runs per arm, medians:
Mutex delay, summed over the engine's goroutines, from separate runs with Go's mutex profiling (2 per arm):
Testing
engine-dev test --pkg ./dagql --race, 1,198 tests. CI runs it on this branch.TestCacheReadPathsShareEgraphLockfails without the change;TestCacheConcurrentSharedClaimsKeepOwnershipExactchecks that concurrent claims under the read lock, interleaved with releases, keep ownership counts exact.The TLA+ model
The cache model in
dagql/tlatreated every critical section as one action. With read sections now able to overlap, that's sound only if they commute, so the model now checks it:SharedSections:"atomic"keeps every existing configuration's state space exactly as before;"split"models the lookup hit and the ID-load claim each as a selection step and a claim step, with other read sections, release marking and lock-free steps free to run between them;"split_stale_edge"is a deliberate mutation that must violateOwnershipExact.shared_claimsconfigurations are new with this PR:shared_claimspasses at its full bounds (two sessions, three invocations, release): about 205 million distinct states.shared_claims_pruneadds prune and persistable requests. With both sessions able to release, it never finished: after 100 minutes at ten workers it was at 270 million distinct states, with the queue still growing. It now lets only one session release, and passes with about 309 million.shared_claimscovers both sessions releasing, without prune.shared_claims_resourcescovers the ID-load claim's split on an imported graph.shared_claims_stale_edgeandshared_claims_prune_stale_edgeboth violateOwnershipExactwithin seconds. Each counterexample has two lookup hits selected and unclaimed at once, which shows those bounds let two hits overlap."atomic", so their state spaces are unchanged. They weren't all rerun for this PR.