Skip to content

fix(server): stop per-session toolsets on session delete - #3884

Open
EronWright wants to merge 1 commit into
docker:mainfrom
EronWright:contrib/stop-toolsets-on-delete
Open

fix(server): stop per-session toolsets on session delete#3884
EronWright wants to merge 1 commit into
docker:mainfrom
EronWright:contrib/stop-toolsets-on-delete

Conversation

@EronWright

Copy link
Copy Markdown
Contributor

fix(server): stop per-session toolsets on session delete

Problem

A session materialised via teamloader owns a team whose toolsets may hold
external resources — notably stdio MCP subprocesses. DeleteSession cancels the
runtime context but never calls team.StopToolSets, and BatchDeleteSessions has
the same gap (LocalRuntime.Close only stops background agents; StopToolSets was
otherwise reached only via the transient GetAgentToolCount). So a deleted session's
MCP subprocesses leak until the server process exits.

Fix

Track the per-session team on activeRuntimes; once the session's stream has
drained, call StopToolSets from both delete paths. The drain runs in a background
goroutine that outlives the originating request, on a detached context
(WithoutCancel) carrying its own sessionDrainTimeout (5 min) budget — the same
budget already bounds the drain wait, so it now bounds the whole teardown (drain +
StopToolSets). Nil for attached runtimes (AttachRuntime), whose toolset lifecycle
belongs to the embedder — a no-op there.

Test

go test ./pkg/server/ green. Verified against a multi-tenant host: the per-session
subprocess count returns to zero on delete and after server shutdown (previously it
stayed pinned until exit).

A session materialised via teamloader owns a team whose toolsets may hold
external resources — notably stdio MCP subprocesses. DeleteSession cancelled
the runtime context but never called team.StopToolSets, so those subprocesses
leaked until the server process exited; BatchDeleteSessions had the same gap.

Track the per-session team on activeRuntimes and, once the session's stream
has drained, call StopToolSets from both delete paths, bounding it by the
originating request's deadline. Nil for attached runtimes (AttachRuntime),
whose toolset lifecycle belongs to the embedder, so it is a no-op there.
@EronWright
EronWright requested a review from a team as a code owner August 2, 2026 00:08
@aheritier aheritier added area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core agent runtime, session management kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants