Skip to content

Fix crash from nulled _session in ChatModel.dispose()#309570

Merged
roblourens merged 3 commits intomainfrom
roblou/fix-session-null-crash
Apr 13, 2026
Merged

Fix crash from nulled _session in ChatModel.dispose()#309570
roblourens merged 3 commits intomainfrom
roblou/fix-session-null-crash

Conversation

@roblourens
Copy link
Copy Markdown
Member

ChatModel.dispose() sets _session = undefined on each request to break back-references for GC. However, the _onDidUpdateViewModel event listener in chatListRenderer.ts accesses template.currentElement.sessionResource, which resolves through this._model.session.sessionResource. When updateViewModel fires during a model switch, template.currentElement may still reference an item from the old (now-disposed) view model, causing:

TypeError: Cannot read properties of undefined (reading 'sessionResource')

Fix: Remove the _session = undefined line. The _requests array is already emptied immediately after (this._requests.length = 0), so the back-references are broken when the requests are GC'd along with the model.

Fixes #309568

(Written by Copilot)

Remove the _session = undefined line that breaks the back-reference
from requests to the ChatModel during dispose. This causes a crash
in chatListRenderer when the _onDidUpdateViewModel event fires and
accesses template.currentElement.sessionResource on a stale item
whose session has been nulled out.

The _requests array is already emptied right after, so the
back-references are broken when the requests are GC'd with the model.

Fixes #309568

(Written by Copilot)
Copilot AI review requested due to automatic review settings April 13, 2026 18:10
@roblourens roblourens enabled auto-merge (squash) April 13, 2026 18:11
@roblourens roblourens added the ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch label Apr 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Screenshot Changes

Base: b81fc85a Current: a9fee84a

Changed (2)

chat/aiCustomizations/aiCustomizationManagementEditor/McpBrowseMode/Light
Before After
before after
editor/inlineCompletions/other/JumpToHint/Dark
Before After
before after

@vs-code-engineering
Copy link
Copy Markdown
Contributor

This PR will be automatically cherry-picked to release/1.116 when merged.

@roblourens roblourens closed this Apr 13, 2026
auto-merge was automatically disabled April 13, 2026 18:17

Pull request was closed

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

Adjusts ChatModel.dispose() to avoid a crash during chat session/model switching by no longer mutating request objects in a way that can break stale view-model references.

Changes:

  • Simplifies dispose() to only dispose per-request responses and fire _onDidDispose.
  • Removes the explicit (r as any)._session = undefined mutation on ChatRequestModel instances during disposal.
  • Removes post-super.dispose() cleanup that previously cleared _requests and nulled other heavy fields.
Show a summary per file
File Description
src/vs/workbench/contrib/chat/common/model/chatModel.ts Modifies ChatModel.dispose() behavior to prevent crashes from stale request/view-model references during session switching.

Copilot's findings

Comments suppressed due to low confidence (2)

src/vs/workbench/contrib/chat/common/model/chatModel.ts:2825

  • The PR description/issue text says the _requests array is still cleared immediately after breaking back-references, but this change removes that cleanup entirely. If the intent is only to stop nulling ChatRequestModel._session, the rest of the disposal cleanup should likely remain; otherwise this is a larger behavioral change than described (and increases the risk of memory retention for disposed models).
	override dispose() {
		this._requests.forEach(r => r.response?.dispose());
		this._onDidDispose.fire();

		super.dispose();

src/vs/workbench/contrib/chat/common/model/chatModel.ts:2825

  • This change fixes a crash that occurs when stale ChatRequestModel instances are still referenced after the owning ChatModel is disposed. There doesn’t appear to be a unit test exercising “dispose model, then access request/sessionResource via an old view-model element” to prevent regressions; consider adding a regression test that disposes a model and then reads request.session.sessionResource (or the view-model getter) to ensure it never throws during session switching.
	override dispose() {
		this._requests.forEach(r => r.response?.dispose());
		this._onDidDispose.fire();

		super.dispose();
  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread src/vs/workbench/contrib/chat/common/model/chatModel.ts
Restore the _requests.length, dataSerializer, and _editingSession
cleanup — these null out fields on the ChatModel itself and are safe.
Only the (r as any)._session = undefined line needs to be removed
since it breaks getters accessed by stale event listeners.

(Written by Copilot)
@roblourens roblourens reopened this Apr 13, 2026
@roblourens roblourens removed the ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch label Apr 13, 2026
@roblourens roblourens merged commit 27fbc63 into main Apr 13, 2026
26 checks passed
@roblourens roblourens deleted the roblou/fix-session-null-crash branch April 13, 2026 20:07
@vs-code-engineering vs-code-engineering bot added this to the 1.117.0 milestone Apr 13, 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.

ChatModel.dispose() nulling _session causes crash in chatListRenderer

3 participants