Skip to content

[backport-0.21] engine: stop cached state from keeping session schema servers alive - #14756

Merged
marcosnils merged 5 commits into
dagger:backport-0.21from
marcosnils:backport-0.21-schema-server-leak
Oct 21, 2026
Merged

marcosnils merged 5 commits into
dagger:backport-0.21from
marcosnils:backport-0.21-schema-server-leak

Conversation

@marcosnils

Copy link
Copy Markdown
Contributor

Reply by Claude, reviewed by Marcos

Backport of #14443 to backport-0.21. Fixes the engine OOM from retained dagql.Server schemas reported on v0.21.9 (#14362).

The merge didn't cherry-pick cleanly: backport-0.21 is missing several subsystems that main has. So the 8 PR commits were picked one at a time and adapted where needed:

Layer Upstream Here
1. Classes hold their server weakly 8181be6300 picked (trivial conflict: no typeDefFilters on 0.21)
2. Cache entries pin a client server eb427da549, 0be0a9aca3 skipped: 0.21 has no snapshot sharing and no partGate.server, so this reference doesn't exist
3. Module dep schemas in a session memo 6cc2db2314, e305e02a0b squashed and adapted (see below)
4. objClass holds its server weakly 08c1e2d63b, 49514cc911 picked (conflicts limited to code that only exists on main)
5. Module resolvers hold their server weakly e2b5460272 adapted: 0.21 has no collection objects, state rebinding or check projections, so only objFun's CallOpts.Server needs the weak fallback. forObject is dropped. dagql.ContextWithServer (from 0be0a9aca3) is added for the test

Layer 3 on 0.21

0.21 has no per-client SchemaBuilderMemo, so this backport adds:

  • SchemaBuilderMemo, SchemaBuilder.ClientOwned and loadSchemaFailed, as on main.
  • A memo per session on daggerSession. It's created with the session and cleared in removeDaggerSession after the session's in-flight dagql work drains. It's exposed through core.Server.SessionSchemaBuilderMemo.
  • SchemaBuilder.Schema resolves builders that aren't client-owned (e.g. Module.Deps) through the session memo. Client servedMods builders are marked client-owned.
  • ModDepsForCall returns 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

  • Upstream tests ported: TestClassesDoNotRetainServer, TestCacheObjClassDoesNotRetainServer, TestModuleObjectClassDoesNotRetainServer, TestInstalledServerFallsBackOnceCollected, and the SchemaBuilderMemo tests in core/moddeps_test.go (TestSchemaBuilderServerLivesInSessionMemo among them).
  • Checked that TestModuleObjectClassDoesNotRetainServer fails when the resolver captures dag strongly.
  • go test passes for ./dagql/..., ./core/, ./core/schema/..., ./core/sdk/... and ./engine/server/....
  • Not run: core/integration suites, or an engine heap measurement on this branch.

🤖 Generated with Claude Code

sipsma and others added 5 commits October 21, 2026 10:09
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
marcosnils force-pushed the backport-0.21-schema-server-leak branch from 33e8e9d to 97f49c7 Compare October 21, 2026 13:11
@marcosnils
marcosnils merged commit 07f779c into dagger:backport-0.21 Oct 21, 2026
76 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