Skip to content

copilotAgentSession: avoid leaking subscriptions when disposed during init#318504

Merged
roblourens merged 1 commit into
mainfrom
agents/saw-this-error-please-fix-error-trying-to-4595d5bd
May 27, 2026
Merged

copilotAgentSession: avoid leaking subscriptions when disposed during init#318504
roblourens merged 1 commit into
mainfrom
agents/saw-this-error-please-fix-error-trying-to-4595d5bd

Conversation

@roblourens
Copy link
Copy Markdown
Member

Problem

Error: Trying to add a disposable to a DisposableStore that has already been disposed of. The added object will be leaked!
    at DisposableStore.add (lifecycle.js:327)
    at CopilotAgentSession._register (lifecycle.js:391)
    at CopilotAgentSession._subscribeForLogging (copilotAgentSession.js:1714)
    at CopilotAgentSession.initializeSession (copilotAgentSession.js:582)
    ...
    at async CopilotAgent._resumeSession
    at async CopilotAgent.getSessionMessages
    at async AgentService.restoreSession
    at async ChangesetSessionCoordinator.restoreSessionIfChangesetSubscription

CopilotAgentSession.initializeSession() is async. Between await this._wrapperFactory(...) resolving and the subsequent _subscribeToEvents() / _subscribeForLogging() calls, the session can be disposed (e.g. via _destroyAndDisposeSession). The subscribe methods then call _register on an already-disposed DisposableStore, producing the warning and leaking the freshly-created SDK session wrapper.

Fix

After the wrapper factory resolves, check this._store.isDisposed. If disposed:

  • dispose the freshly-created wrapper (otherwise the disposed store warns and drops it on _register, leaking it), and
  • throw CancellationError so callers (_materializeProvisional / _resumeSession) treat it as a cancellation rather than producing a half-initialized session.

(Written by Copilot)

… init

If CopilotAgentSession is disposed while initializeSession() is awaiting
the wrapper factory, the subsequent _subscribeToEvents() /
_subscribeForLogging() calls would invoke _register on an already-
disposed DisposableStore, producing 'added object will be leaked'
warnings and leaking the SDK session wrapper.

After the factory resolves, check whether the store has been disposed;
if so, dispose the freshly-created wrapper and throw CancellationError
so callers (_materializeProvisional / _resumeSession) treat it as a
cancellation rather than producing a half-initialized session.

(Written by Copilot)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 27, 2026 04:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a disposal race in CopilotAgentSession.initializeSession() where the session can be disposed while awaiting the SDK wrapper factory, causing _register() to be called on an already-disposed DisposableStore (warning + leaked wrapper/subscriptions). The change disposes the newly-created wrapper when this happens and throws CancellationError so callers treat the outcome as a cancellation rather than a partially-initialized session.

Changes:

  • Create the SDK wrapper without immediately registering it, allowing a disposal check right after factory resolution.
  • If the session was disposed during the await, dispose the wrapper and throw CancellationError.
  • Otherwise register the wrapper and proceed to subscribe to events/logging as before.
Show a summary per file
File Description
src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts Adds a post-factory disposal check to prevent registering/subscribing on a disposed store; disposes wrapper and throws CancellationError on late disposal.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts
@DonJayamanne
Copy link
Copy Markdown
Contributor

thanks was getting a model to look into this issue myself

@roblourens roblourens merged commit 4a5c2b8 into main May 27, 2026
26 checks passed
@roblourens roblourens deleted the agents/saw-this-error-please-fix-error-trying-to-4595d5bd branch May 27, 2026 16:23
@vs-code-engineering vs-code-engineering Bot added this to the 1.123.0 milestone May 27, 2026
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.

3 participants