Skip to content

dagql: defer session release until in-flight operations finish - #13940

Merged
sipsma merged 7 commits into
mainfrom
sipsma/remote-cache-track4-deferred-release
Sep 3, 2026
Merged

sipsma merged 7 commits into
mainfrom
sipsma/remote-cache-track4-deferred-release

Conversation

@sipsma

@sipsma sipsma commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The dagql cache could not see its own in-flight work. Session release could therefore collect a result after a cache operation claimed it but before that operation returned, including work from detached executors that the server handler drain cannot count. The same gap allowed shutdown persistence to observe transient publication ownership.

This is layer four of the cache-remediation stack and depends on #13938, #13936, and #13934.

Design

  • Add a per-session lifecycle record that packs the released tombstone and active-operation count into one atomic word. Operation admission and release are linearized with compare-and-swap without taking a cache-wide mutex.
  • Count operations at every value-bearing cache boundary, including call lookup/initialization, result attachment and ID loads, Evaluate waiters, each lazy callback attempt, snapshot lease synchronization, and the arbitrary-value cache.
  • Mark a session dead immediately in ReleaseSession. If work is active, cleanup is irrevocably assigned to the last exiting operation; if no work is active, release performs cleanup immediately.
  • Refuse a value-bearing success at operation exit when release won the race, so detached work cannot return a result whose cleanup is eligible.
  • Make Cache.Close reject new work, wait for operation and deferred-cleanup quiescence on its caller context, and fail dirty if the context expires or deferred cleanup failed. It never takes a timed-out snapshot or writes a clean marker after such a failure.
  • Give lazy callbacks their own attempt-lifetime token, held until the callback goroutine actually exits rather than only until its waiters return. The token covers both stages of an attempt (callback body, then cache-side bookkeeping) and bookkeeping-only retry attempts.
  • Refuse telemetry span capture for released sessions. The two span writers now match the other per-session map writers, so no writer can recreate a released session's maps. Under this layer's operation accounting every current caller is already enclosed by a counted operation; the guards keep that safe independent of future callers.
  • Check mutation-helper registration by ID membership. Numeric result IDs are engine-lifetime unique (dagql: make session ownership claims atomic #13934), so the four e-graph mutation helpers guard against operating on a collected result with a plain registration check instead of pointer identity.

Contract and cost

  • ReleaseSession returning now means the session is dead and cleanup is complete or irrevocably assigned. Cleanup hooks may run later on another caller goroutine.
  • Deferred cleanup errors are logged when observed, accumulated by the cache, and returned from Cache.Close.
  • New claims and late value-bearing returns for released sessions fail with ErrCacheSessionReleased.
  • Immediate before/after cache-size logging during session teardown is approximate because cleanup may be deferred.
  • The uncontended hot path adds one per-session map lookup and two atomic compare-and-swaps per counted operation, with no cache-wide mutex acquisition.
  • Per-session lifecycle records remain for the engine lifetime. This is intentional because server session IDs are single-use and retaining the tombstone closes the admission race.

Model

The TLA+ model now faithfully represents operation admission and exit, deferred release, shutdown quiescence, and lazy callback attempt tokens, layered over the current two-stage lazy attempt model (callback body, then cache-side bookkeeping). The four former expected-failure configurations for release-in-flight, drain escape, flush-in-flight, and flush-with-drain are strengthened green gates, with a conditional liveness property that assigned cleanup eventually completes. A new lazy_release configuration gates callback-token deferral.

Invariant coverage was widened after review: every release-enabled configuration now checks the live-return property, and the lazy_import configuration (the only one combining release, lazy work, import, and flush) gates the return-boundary and token-deferral properties.

All 25 configurations are green. A one-time exhaustive three-evaluator run of the lazy configuration also passed, at 78,792,507 distinct states; the configuration stays at two evaluators for CI cost.

Validation

  • go test ./dagql -count=1
  • go test ./engine/server -count=1
  • Targeted go test -race ./dagql coverage for operation counting, deferred release, lazy callback lifetime, arbitrary-value cleanup, shutdown cancellation, and released-session span capture
  • go vet ./dagql ./engine/server — the dagql package was clean; engine/server reported one pre-existing unrelated context-cancel finding
  • dagger check tla-check:cache-lifecycle — 25 configurations green
  • dagger check dagger-go-sdk:generate

No integration test suite was run.

@sipsma sipsma changed the title sipsma/remote cache track4 deferred release dagql: defer session release until in-flight operations finish Aug 20, 2026
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from 4dddcf0 to 7693ac9 Compare August 21, 2026 19:22
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from e8e6e3b to 5709595 Compare August 26, 2026 04:30
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from 5709595 to dfaab9d Compare August 26, 2026 04:30

@shykes shykes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#rubberstamp

@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from dfaab9d to 5fafb10 Compare August 27, 2026 02:10
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from 5fafb10 to 2d1f7c9 Compare August 30, 2026 19:38
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from 2d1f7c9 to d81560e Compare September 3, 2026 22:16
Base automatically changed from sipsma/remote-cache-track3-lazy-retry to main September 3, 2026 23:05
Numeric result IDs are engine-lifetime unique, so the four e-graph
mutation helpers' lock-time guards can ask whether the caller's result
ID is still registered instead of comparing registry pointers. This
removes the last registry pointer-identity comparisons in dagql
production code.

Signed-off-by: Erik Sipsma <[email protected]>
The two telemetry span writers took sessionMu without checking the
session tombstone, unlike the five sibling per-session map writers,
which all refuse released sessions. A span write landing after release
cleanup would recreate per-session map entries that nothing ever
deletes again, since session IDs are single-use.

With this layer's operation accounting, every current caller of these
writers runs inside a counted operation, and cleanup is deferred until
the last operation exits, so no current path writes after cleanup. The
guards are defense in depth: they keep these writers safe independent
of future token coverage, and they close the window outright for any
engine running the earlier session-lifecycle layers without this one.

Signed-off-by: Erik Sipsma <[email protected]>
Both cleanup reviewers audited invariant coverage against enabled
machinery and converged on the same gaps. Every addition is an
invariant line on an existing configuration; no model state changes.

- lazy_import gains ReturnedLive, NoUnderflow, and
  LazyAttemptDefersCollection: it is the only configuration combining
  release, lazy work, import, and flush, and it previously gated none
  of the return-boundary or token-deferral properties.
- Every release-enabled configuration now checks ReturnedLive, which
  this layer's late refusal makes hold everywhere.
- drain_escape and lazy_release gain OwnershipExact; both run the
  deferred-cleanup cascade.
- lazy_release gains EvalDoneComplete, LazyCompleteSettled, and
  LazyMutualExclusion, matching the other lazy safety gates.

All fourteen widened configurations pass.

Signed-off-by: Erik Sipsma <[email protected]>
tla-check gains One: run a single TLC configuration through the same
pinned jar and invocation as the CacheLifecycle check, returning the
raw TLC output with no expectation applied. An optional invariant
replaces the configuration's INVARIANTS line (safety-only, PROPERTY
dropped), and an optional define appends a scratch operator to the
spec, so reachability probes and red-proof runs go through Dagger
instead of a host-side jar.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/remote-cache-track4-deferred-release branch from d81560e to f6f4856 Compare September 3, 2026 23:05
@sipsma
sipsma merged commit e0c59a4 into main Sep 3, 2026
89 checks passed
@sipsma
sipsma deleted the sipsma/remote-cache-track4-deferred-release branch September 3, 2026 23:48
@danielwegener

Copy link
Copy Markdown

will this be backported to 0.26? Its hitting us quite hard (clogging up shared engine)

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.

3 participants