copilotAgentSession: avoid leaking subscriptions when disposed during init#318504
Merged
roblourens merged 1 commit intoMay 27, 2026
Merged
Conversation
… 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>
Contributor
There was a problem hiding this comment.
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 throwCancellationError. - 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
Contributor
|
thanks was getting a model to look into this issue myself |
DonJayamanne
approved these changes
May 27, 2026
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.
Problem
CopilotAgentSession.initializeSession()is async. Betweenawait this._wrapperFactory(...)resolving and the subsequent_subscribeToEvents()/_subscribeForLogging()calls, the session can be disposed (e.g. via_destroyAndDisposeSession). The subscribe methods then call_registeron an already-disposedDisposableStore, producing the warning and leaking the freshly-created SDK session wrapper.Fix
After the wrapper factory resolves, check
this._store.isDisposed. If disposed:_register, leaking it), andCancellationErrorso callers (_materializeProvisional/_resumeSession) treat it as a cancellation rather than producing a half-initialized session.(Written by Copilot)