Skip to content

engine: stop cached state from keeping session schema servers alive - #14443

Merged
sipsma merged 8 commits into
dagger:mainfrom
sipsma:sipsma/schema-server-leak
Oct 1, 2026
Merged

sipsma merged 8 commits into
dagger:mainfrom
sipsma:sipsma/schema-server-leak

Conversation

@sipsma

@sipsma sipsma commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Long-running engines retain dagql schema servers (*dagql.Server) after the sessions that built them have ended. This shows up as a Go heap that keeps growing until the engine is OOM-killed (#14362, on v0.21.9). In the reported heap snapshots, about 70% of the live heap is schema state: per-client forks of the core schema, module dependency schemas, and the ast.Schema built for each. DAGQL metadata pruning can't see this memory, so lowering its limit had no effect.

Each dagger call builds about 11 schema servers. In a test with two small Dang modules whose source changed each session, about 9 of them per session stayed alive for as long as that session's results were cached. Heap dumps, plus experiments that replaced one reference at a time with a weak one, found five independent references that keep them alive. This PR removes each one; each layer is a separate commit range.

Changes

  1. Types don't keep their server alive (8181be6300). Every dagql.Class stored srv.invalidateSchemaCache, a method value that holds a strong pointer to the whole server. Classes ride on every ObjectResult, and cached values embed object results, for example a container's parent. Classes now hold a weak.Pointer[Server], created once per server, and invalidate only while the server is alive.

  2. Cache entries no longer pin a client's server (eb427da549, 0be0a9aca3). registerLazyEvaluation stored the resolver's server on effectively every cache entry, in partGate.server. It was read only as a fallback when the context had no server. In engine flows the only such path is the snapshot-sharing worker on remote-cache engines; client-driven demand always carries a server, and lazy tasks keep it. The sharing worker now binds its engine-lifetime schema-only server for every typed slot. Before, it did so only for slots with services, even though core Container/Directory/File decodes also need a server. The pointer and both fallbacks are removed, and dagql.ContextWithServer lets tests bind a server. dagger.io's hack/e2e TestProtocol passed against an engine built from this layer (before rebasing onto current main).

  3. Module dependency schemas move from the module to a session memo (6cc2db2314, e305e02a0b). Module.Deps cached its built server forever, and cached function results keep their Module alive. Only client- or session-owned builders now keep a built server. A module's Deps resolves its schema through a memo on the dagger session, dropped at session teardown. Handle loads (ModDepsForCall) keep using the per-client memo. A per-client memo alone rebuilt the callee's schema on every cross-module function call, because each such call runs in a fresh nested client; the session memo avoids that.

  4. Cached results don't keep their type's server alive (08c1e2d63b, 49514cc911). sharedResult.objClass kept the first wrapping class for the life of the entry. It now stores the type name plus a weak reference to the class's server. Cross-module hits look the class up there while that server is alive, and otherwise fall back to resultServerForCall, as before. toSelectable now returns that fallback's error instead of reporting "unknown type".

  5. Module resolvers don't keep their server alive (e2b5460272). Module object types' resolver closures captured the server they were installed into. Module objects with classes are embedded in other cached values, such as ModuleObject.Fields, Check.Receiver, interface values and artifact results, so that pinned one served schema per session. The resolvers now hold their server weakly. Once it's gone, resolvers that only need a core server use the current server, and those that need the module's types use the same module-aware lookup as resultServerForCall.

Measurements

Dev engine with disk GC off, counting live dagql.Servers by creation site after a forced GC (temporary instrumentation, not in this PR):

Workload Before With this PR
10 sessions, module source changing each session: schema servers retained after the sessions end about 9 per session 0
Same: heap growth over the 10 sessions +22 MiB +8 MiB
Go ifaces fixture (cross-module interfaces): served schemas retained per run 1 0

The cross-module call chains in one session needed 5 module-dependency schema builds, the same as before. Cache hits after GC still work through the fallback paths.

Building a core fork plus its schema takes about 8 ms (benchmark), so rebuilding instead of retaining is cheap.

Testing

  • New tests for each layer, each failing without its change. Among them: TestClassesDoNotRetainServer, TestSnapshotSharingTypedReceiverDecodesWithPreparationServer, TestSchemaBuilderServerLivesInSessionMemo, the objClass retention test, and TestModuleObjectClassDoesNotRetainServer.
  • go test passes for ./dagql, ./core, ./core/schema/..., ./core/sdk/... and ./engine/server, plus -race on ./dagql; lint is clean.
  • The remote-cache transfer suite and TestCachedAcrossClients were run on an engine built at layer 2.
  • Not yet run on the full stack: the heavy core/integration module suites, and the remote-cache e2e for layers 3–5.

Not in this PR

  • The engine doesn't set GOMEMLIMIT, so with the default GOGC=100 a container holds about 2× its live heap. That's worth a separate change.
  • The introspection JSON for each distinct module schema (about 0.5 MB) is held in memory by the cached schema file result, and the prune estimate doesn't count it.

@sipsma
sipsma marked this pull request as ready for review October 1, 2026 04:31
@sipsma
sipsma force-pushed the sipsma/schema-server-leak branch from 0d79a48 to fd544a7 Compare October 1, 2026 04:31
@marcosnils marcosnils self-assigned this Oct 1, 2026
@marcosnils
marcosnils self-requested a review October 1, 2026 17:09
Comment thread dagql/server.go Resolved
sipsma added 8 commits October 1, 2026 18:17
Every Class stored srv.invalidateSchemaCache, a method value holding a
strong pointer to its whole *Server. Classes are carried by object
results, and cached results can outlive the (typically per-client) fork
that wrapped them, so any cached result kept that fork, its classes and
its built schemas alive.

Classes now hold a weak pointer to their server, made once per server,
and invalidate its schema cache only while it is still alive. Anything
that can read a server's schema cache holds a strong reference to it, so
skipping invalidation for a collected server is unobservable.

Signed-off-by: Erik Sipsma <[email protected]>
A snapshot share slot bound the engine's schema-only preparation server
only when its donated descriptor carried Services. Every other typed
slot ran on the worker's bare context, so its store preparation decoded
with whatever server partDecodeContext fell back to: the one stored on
the receiver's row, i.e. the client server that first produced it. Core
File, Directory and Container stores need a server there, to resolve
their snapshot roles and referenced rows.

Bind the registered preparation server for every typed slot. It is an
engine-lifetime fork of the core schema, which is all a part store
decodes, and it belongs to no client. Encoded receivers still decode
nothing and bind nothing; with no callback registered, service-free
typed slots stay eligible and decode with no server, as before.

Signed-off-by: Erik Sipsma <[email protected]>
registerLazyEvaluation stored the resolver's *Server on the row's part
gate for every result it registered, so each cache entry kept the first
(typically per-client) server fork that produced it alive, with its
whole schema, long after that client's session ended.

The pointer was read only as a fallback when a part decode or a
delegated-parent load ran without a server in context. Foreground
demand and lazy tasks always carry the requesting client's server
(DetachClientScope keeps context values), the remote-cache adapter's
export, merge and offer paths decode nothing, and snapshot sharing now
binds its own schema-only server. What remained were unit tests driving
part APIs from bare contexts.

Drop the pointer, both fallbacks and the resolver parameter
registerLazyEvaluation grew for it. A decode that needs a server and
has none fails where it needs it; a delegated-parent load without one
returns an explicit error instead of passing a nil *Server on as a
TypeResolver. Add ContextWithServer so tests outside dagql can build
the context a resolver sees, and use it in the core and core/schema
test fixtures that drive part APIs directly.

Signed-off-by: Erik Sipsma <[email protected]>
A Module's Deps builder cached the server it built forever. Modules are
loaded per client, and cached function results keep their Module alive
(through ModuleObject.Module, module classes and dependency edges), so
every client's copy of each module's dependency schema stayed alive
long after the client was gone.

Only a client-owned builder now keeps the server it builds: a client's
SchemaBuilderMemo entry, or the builder the client runtime serves from.
Schema on any other builder resolves through the current client's memo,
keyed by module set as before, so a module's dependency schema is still
built at most once per client and is dropped with the client's other
heavy state. The memo stores a client-owned copy rather than the
caller's builder, and ownership is never inherited by a derived
builder, so nothing stored on a cached value can hold a built server.
Without a client in context the schema is built for that call alone.

Signed-off-by: Erik Sipsma <[email protected]>
Resolving a Module's Deps schema through the current client's memo
rebuilt it on every function call made from inside another module: each
such call runs in a fresh nested client with an empty memo, and SDK
runtimes ask for the callee's dependency schema on every call. One
session making eight cross-module calls built twelve dependency schemas,
against five when the schema was cached on the Module.

Keep those schemas in a memo on the dagger session instead, shared by
all of its clients and cleared once its client scopes have drained.
A module set's dependency schema is now built at most once per session,
and still none outlives the session. ModDepsForCall keeps using the
per-client memo, so each nested client's handle-load entries are still
dropped with that client rather than kept for the whole session.

Signed-off-by: Erik Sipsma <[email protected]>
A shared result remembered the class that first wrapped it (objClass)
for the life of the cache entry, so a reader whose schema lacks the
type could re-wrap it without rebuilding a module-aware schema. Module
classes have field resolvers that capture the server they were
installed into, so every cached module-typed result kept the served
schema of the session that created it alive.

Remember the class by type name and a weak pointer to its server
instead, and look it up on that server while it is alive. Once it is
gone, reconstruction falls back to rebuilding a schema from the
result's call graph (resultServerForCall), as it already does for
results whose class was never captured, and caches the resolved class
back the same weak way. A class whose server is gone no longer blocks
that cache-back.

Signed-off-by: Erik Sipsma <[email protected]>
A module object type's resolvers captured the server they were installed
into. The type's class rides along with its object results wherever they
go: another module object's field values, a check's receiver, an
interface value. So a cached module object holding another module's
object kept the session's whole served schema alive.

Hold that server weakly, and fall back by what each resolver needs it
for once it is gone. Functions only apply ignore patterns to Directory
args and check projections only wrap a core Check, so any server with
the core schema will do: they use the server running the current call.
State rebinding loads and wraps the module's own objects and collection
lists select on them, so they rebuild a schema with the receiver's
module installed from its call graph, through the client's schema memo,
as cache reconstruction already does.

Constructors, entrypoint proxies and the with field live on the served
schema's Query class, which no cached result carries, so they keep their
server as before.

Signed-off-by: Erik Sipsma <[email protected]>
With cached results holding their type's server weakly, toSelectable's
call-graph fallback runs whenever that server has been collected. It
dropped the fallback's error and reported "unknown type", hiding the
real cause. Return it, as wrapSharedResultWithResolver already does.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/schema-server-leak branch from fd544a7 to 49514cc Compare October 1, 2026 18:18
@sipsma
sipsma merged commit ed8f8b8 into dagger:main Oct 1, 2026
90 checks passed
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