Repository navigation
Conversation
Reading the model catalogue or the provider list builds `createAgentSessionServices()`, which imports and runs every configured extension. Nothing ever unloaded them: `AgentSessionServices`, `ResourceLoader` and `LoadExtensionsResult` expose no dispose, and the only release path is the `session_shutdown` event, which only a *session's* extension runner can emit (the loader's `runtime` is a throwing stub until a runner initializes it). So each read left the extension's own resources alive for the life of the server process — a helper process, for an extension such as AFT — and the memory its load allocated until a GC happened to reclaim it. Every read that enumerates models or providers now goes through `withExtensionServices()` / `withExtensionRuntime()`. They create a throwaway in-memory session (`SessionManager.inMemory`, `noTools: "all"`, so no session file and no tool can run), hand the services to the read, and then emit `session_shutdown` and dispose the session. Extensions that clean up on that event now get it while the read is still the reason they were loaded. Measured on the deployment host, three rounds per variant (and six for the wrapper): - the load costs 448-532 ms warm (~3.6 s cold); the session, the shutdown event and the dispose together add 2-41 ms; - the wrapper released the extension's helper process every round (the plain path left one more alive each round: 0 -> 1 -> 2 -> 3); - heap and external memory returned to their pre-read level and stayed flat across rounds; the loaded extension sets stayed collectable, with at most the two most recent still reachable. A plugin that hangs in its handler cannot hang a read: the emit is raced against a 5 s timeout and the session is disposed either way. If the throwaway session cannot be created at all, the read still answers and logs why it could not release anything. Behaviour: the throwaway session emits `session_start` (the SDK's default for a new session), so an extension sees one more start/shutdown pair per read — the release it never got before is the point.
HoloNova
marked this pull request as ready for review
September 30, 2026 04:27
CI runs the release test on a Node version whose MockTimers ExperimentalWarning reaches console.error, so asserting on the first captured call failed there while passing here. Assert that the message was logged at all instead.
This was referenced Sep 30, 2026
Owner
|
Thanks @HoloNova. The leak is real for extensions that start work in their factory:
A fix that doesn't pretend to be a session would be welcome. For example, build the extension-backed services once per Generated by Claude Code |
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.
What
Reading the model catalogue (
GET /api/models), the provider list(
GET /api/auth/providers), the enabled-models view, the default-preferencecheck and the catalog refresh all build
createAgentSessionServices()— whichimports and runs every configured extension, because that is where
extension-registered providers come from.
Nothing ever unloaded them:
AgentSessionServices,ResourceLoaderandLoadExtensionsResultexpose nodispose;
session_shutdownevent, and only a session'sextension runner can emit it (the loader's
runtimeis a throwing stub untila runner initializes it).
So every such read left the extension's own resources alive for the life of the
server process — a helper process, for an extension such as AFT — plus whatever
its load allocated until a GC happened to run. Opening the Models panel three
times meant three leftover helper processes.
How
lib/model-runtime.tsnow owns the only way to get that runtime:The wrapper builds the services, creates a throwaway in-memory session
(
SessionManager.inMemory,noTools: "all"— no session file, no tool canrun), hands the services to the read, and then emits
session_shutdownanddisposes the session in a
finally.Callers updated:
/api/models,/api/models/enabled,/api/models/default,/api/auth/providers,/api/auth/api-key/[provider],/api/auth/login/[provider],/api/auth/logout/[provider],lib/model-catalog-refresh.ts.createModelRuntimeWithExtensions()is gone:leaving it in place would leave the leak reachable.
Numbers (deployment host, capped scopes)
Cold start is ~3.6 s; the wrapper's own session, shutdown event and dispose
together add 2-41 ms. Heap and
externalreturned to their pre-read level andstayed flat across six rounds; loaded extension sets stayed collectable, at most
the two most recent still reachable.
Robustness
against a 5 s timeout and the session is disposed either way (logged).
logs that it could not release anything.
finally).Behaviour change
The throwaway session emits
session_start(the SDK default for a new session),so an extension sees one more start/shutdown pair per read. That is the intended
trade: it is also the first time these reads tell an extension that its work is
done.
Verified
Whole suite: 1330 tests, 0 failures;
tsc --noEmitandeslint .clean. Newcoverage in
lib/model-runtime.test.mjs(seven tests, SDK stubbed): in-memorysession with
noTools: "all",session_shutdownbefore dispose, release whenthe read throws, dispose when the handler throws, release when the handler
never finishes, read still answered when the session cannot be created, and a
guard that no route calls
createAgentSessionServices()directly any more.