Repository navigation
[backport-0.21] engine: stop cached state from keeping session schema servers alive - #14756
Merged
marcosnils merged 5 commits intoOct 21, 2026
Merged
marcosnils merged 5 commits into
marcosnils merged 5 commits into
Conversation
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]> (cherry picked from commit 8181be6)
A Module's Deps builder cached the server it built forever. Cached function results keep their Module alive (through ModuleObject.Module, module classes and dependency edges), so every copy of each module's dependency schema stayed alive long after the session that built it was gone. Only a client-owned builder now keeps the server it builds: a session SchemaBuilderMemo entry, the builder a client serves from, or the builder ModDepsForCall hands to a single load. Schema on any other builder, notably a Module's Deps, resolves through a memo on the dagger session, keyed by module set, so a module set's dependency schema is built at most once per session, shared by its nested clients, and dropped once the session's dagql work has drained. Without a session in context the schema is built for that call alone. Backport of 6cc2db2 and e305e02, adapted for 0.21: there is no per-client SchemaBuilderMemo on this branch, so ModDepsForCall keeps building per call (now as a client-owned builder) instead of using one. Signed-off-by: Erik Sipsma <[email protected]> (cherry picked from commit 6cc2db2) (cherry picked from commit e305e02) Co-Authored-By: Claude Opus 5.5 <[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]> (cherry picked from commit 08c1e2d)
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. Adapted for 0.21: this branch has no collection objects, state rebinding or check projections, so only the function resolvers need the weak fallback, and forObject is dropped. Adds dagql.ContextWithServer for the test. Signed-off-by: Erik Sipsma <[email protected]> (cherry picked from commit e2b5460) Co-Authored-By: Claude Opus 5.5 <[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]> (cherry picked from commit 49514cc)
marcosnils
force-pushed
the
backport-0.21-schema-server-leak
branch
from
October 21, 2026 13:11
33e8e9d to
97f49c7
Compare
1 task
1 task
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.
Backport of #14443 to
backport-0.21. Fixes the engine OOM from retaineddagql.Serverschemas reported on v0.21.9 (#14362).The merge didn't cherry-pick cleanly:
backport-0.21is missing several subsystems that main has. So the 8 PR commits were picked one at a time and adapted where needed:8181be6300typeDefFilterson 0.21)eb427da549,0be0a9aca3partGate.server, so this reference doesn't exist6cc2db2314,e305e02a0bobjClassholds its server weakly08c1e2d63b,49514cc911e2b5460272objFun'sCallOpts.Serverneeds the weak fallback.forObjectis dropped.dagql.ContextWithServer(from0be0a9aca3) is added for the testLayer 3 on 0.21
0.21 has no per-client
SchemaBuilderMemo, so this backport adds:SchemaBuilderMemo,SchemaBuilder.ClientOwnedandloadSchemaFailed, as on main.daggerSession. It's created with the session and cleared inremoveDaggerSessionafter the session's in-flight dagql work drains. It's exposed throughcore.Server.SessionSchemaBuilderMemo.SchemaBuilder.Schemaresolves builders that aren't client-owned (e.g.Module.Deps) through the session memo. ClientservedModsbuilders are marked client-owned.ModDepsForCallreturns a client-owned builder. On main it goes through the per-client memo, but that memo doesn't exist here. A client-owned builder keeps 0.21's current build-per-load behavior and doesn't add an entry to the session memo for every nested client.Testing
TestClassesDoNotRetainServer,TestCacheObjClassDoesNotRetainServer,TestModuleObjectClassDoesNotRetainServer,TestInstalledServerFallsBackOnceCollected, and theSchemaBuilderMemotests incore/moddeps_test.go(TestSchemaBuilderServerLivesInSessionMemoamong them).TestModuleObjectClassDoesNotRetainServerfails when the resolver capturesdagstrongly.go testpasses for./dagql/...,./core/,./core/schema/...,./core/sdk/...and./engine/server/....core/integrationsuites, or an engine heap measurement on this branch.🤖 Generated with Claude Code