Skip to content

dagql: make session ownership claims atomic - #13934

Merged
sipsma merged 3 commits into
mainfrom
sipsma/remote-cache-track1-session-edges
Aug 26, 2026
Merged

sipsma merged 3 commits into
mainfrom
sipsma/remote-cache-track1-session-edges

Conversation

@sipsma

@sipsma sipsma commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Make dagql session ownership claims atomic, remember released sessions for the engine lifetime, and reject claims that arrive after release. Apply the same lifecycle to the arbitrary-value cache, remove three unsafe failed-read rollbacks, and make server session IDs single-use after successful teardown.

This branch lives in dagger/dagger because it is the bottom layer of a planned same-repository pull request stack. This pull request targets main; later dependent layers can target this branch.

Problem

The cache represents a session's ownership of a result in two places: a record in sessionResultIDsBySession and one unit in the result's incoming ownership counter. Those were written in separate locked steps, and the cache did not know whether a session had already been released.

That left several harmful interleavings:

  • A counter unit could land after session release had deleted the corresponding record. Nothing could release that unit, permanently retaining the result and treating it as actively used so pruning would protect it.
  • Session release could decrement a recorded edge before that edge's counter unit had landed. The decrement could consume a publication hold or another owner's unit, drive the counter negative, and collect a result that was still in use.
  • Failed cache reads rolled ownership back by deleting the session record unconditionally while decrementing the counter only when that particular read created the edge. Depending on ordering, this either orphaned a counter unit or removed an edge that another live caller in the same session still relied on.

The arbitrary-value cache had the same split record-and-counter structure.

Changes

  • Add one shared result-claim helper that records the session edge and increments ownership in one egraphMu then sessionMu critical section. Convert request lookup, digest lookup, result-ID lookup, waiter publication, AttachResult, attached DoNotCache results, and list-element access to it.
  • Add the equivalent atomic claim under callsMu then sessionMu for arbitrary cached values.
  • Keep an engine-lifetime cache tombstone for every released session ID and return a clear error from claims that observe it.
  • Delete the failed-read rollback from request lookup, digest lookup, and result-ID loading. A failed read now keeps its session edge until the session is released.
  • Keep server-side released-session IDs under the session registry lock. A session ID is retired atomically with registry removal after teardown, and future attempts to reuse it receive a permanent error. Failed initialization still permits retry with the same ID, and requests arriving during teardown keep the existing retryable error.
  • Add deterministic, channel-coordinated tests for both claim/release orderings, exact ownership recounting, final-waiter handoff release, arbitrary-value parity, all three failed-read paths, and server session retirement.

Behavior changes

  • A cache claim after its session has been released returns an error.
  • A failed cache read retains its session edge until session release.
  • Reusing a session ID after successful teardown is refused with a permanent error for the remainder of that engine process.

Model

Update the TLA+ cache lifecycle specification to model result claims as one atomic action while keeping session release as its two real critical sections. Refusal is a distinct terminal outcome, and final refused waiters still use the normal handoff-release action so late persistence intent is repaired before temporary ownership is dropped.

Five former expected violations are now green regression gates: post-release orphan claims, release consuming another ownership unit, nested post-drain orphan claims, failed-read rollback after cancellation, and failed persisted-decode rollback. The separate returned-after-release problem remains out of scope: the release_inflight and drain_escape configurations still violate ReturnedLive exactly as expected.

Validation

  • go test ./dagql -count=1
  • go test ./engine/server -count=1
  • go test -race ./dagql -run 'TestCache(SessionResultClaimAndReleaseAreAtomic|RefusedFinalWaiterRepairsPersistenceBeforeHandoffRelease|ArbitrarySessionClaimAndReleaseAreAtomic|ArbitraryRefusedOwnerReleasesUnownedValue|ReleasedSessionErrorIncludesSessionID)$' -count=1
  • go test -race ./dagql -run '^TestCacheArbitrary' -count=1
  • go test -race ./engine/server -run 'Test(SameIDConnectDuringBackgroundTeardownGetsRetryable|ReapAbandonedWhenMainClientReconnects|FailedSessionInitializationCanRetrySameID|RetireSessionIsPointerConditional|MainClientLastDisconnectDoesNotBlockOnTeardown)$' -count=1
  • dagger check tla-check:cache-lifecycle

Record session ownership and its counter unit in one critical section, retain engine-lifetime release tombstones, and reject claims from released sessions. Remove failed-read edge rollbacks, apply the same lifecycle to arbitrary values, and make server session IDs single-use after teardown.

Update the cache lifecycle model and add deterministic ownership, rollback, refusal, and server lifecycle coverage.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma marked this pull request as ready for review August 20, 2026 02:31
nextSharedResultID no longer resets to zero when the e-graph empties, so
a numeric result ID can never name two different results within one
engine process. Import's counter write is forward-only for the same
reason. With that invariant, every registry identity check becomes a
plain membership check by ID instead of a pointer comparison.

The arbitrary-value cache gets its own engine-lifetime-unique entry ID,
allocated under callsMu, replacing the pointer comparisons on its
call-key-keyed maps while still protecting a successor entry under a
reused call key from deletion by a stale holder.

Also corrects the spec header's stale description of the arbitrary-value
cache claim shape.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit d3c3b23 into main Aug 26, 2026
89 checks passed
@sipsma
sipsma deleted the sipsma/remote-cache-track1-session-edges branch August 26, 2026 04:30
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