Repository navigation
engine: stop cached state from keeping session schema servers alive - #14443
Merged
Merged
Conversation
sipsma
marked this pull request as ready for review
October 1, 2026 04:31
sipsma
force-pushed
the
sipsma/schema-server-leak
branch
from
October 1, 2026 04:31
0d79a48 to
fd544a7
Compare
marcosnils
self-requested a review
October 1, 2026 17:09
marcosnils
reviewed
Oct 1, 2026
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
force-pushed
the
sipsma/schema-server-leak
branch
from
October 1, 2026 18:18
fd544a7 to
49514cc
Compare
marcosnils
approved these changes
Oct 1, 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.
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 theast.Schemabuilt for each. DAGQL metadata pruning can't see this memory, so lowering its limit had no effect.Each
dagger callbuilds 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
Types don't keep their server alive (
8181be6300). Everydagql.Classstoredsrv.invalidateSchemaCache, a method value that holds a strong pointer to the whole server. Classes ride on everyObjectResult, and cached values embed object results, for example a container's parent. Classes now hold aweak.Pointer[Server], created once per server, and invalidate only while the server is alive.Cache entries no longer pin a client's server (
eb427da549,0be0a9aca3).registerLazyEvaluationstored the resolver's server on effectively every cache entry, inpartGate.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, anddagql.ContextWithServerlets tests bind a server. dagger.io'shack/e2eTestProtocolpassed against an engine built from this layer (before rebasing onto current main).Module dependency schemas move from the module to a session memo (
6cc2db2314,e305e02a0b).Module.Depscached its built server forever, and cached function results keep theirModulealive. Only client- or session-owned builders now keep a built server. A module'sDepsresolves 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.Cached results don't keep their type's server alive (
08c1e2d63b,49514cc911).sharedResult.objClasskept 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 toresultServerForCall, as before.toSelectablenow returns that fallback's error instead of reporting "unknown type".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 asModuleObject.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 asresultServerForCall.Measurements
Dev engine with disk GC off, counting live
dagql.Servers by creation site after a forced GC (temporary instrumentation, not in this PR):ifacesfixture (cross-module interfaces): served schemas retained per runThe 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
TestClassesDoNotRetainServer,TestSnapshotSharingTypedReceiverDecodesWithPreparationServer,TestSchemaBuilderServerLivesInSessionMemo, the objClass retention test, andTestModuleObjectClassDoesNotRetainServer.go testpasses for./dagql,./core,./core/schema/...,./core/sdk/...and./engine/server, plus-raceon./dagql; lint is clean.TestCachedAcrossClientswere run on an engine built at layer 2.core/integrationmodule suites, and the remote-cache e2e for layers 3–5.Not in this PR
GOMEMLIMIT, so with the defaultGOGC=100a container holds about 2× its live heap. That's worth a separate change.