Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 3, 2025

Fixes #264680 - Chat breaks when closing window with chat session

Problem

When opening a chat session in a new window via "Open in New Window", both the original window and new window maintain references to the same chat session. When the auxiliary window is closed, its InstantiationService gets disposed, but the original window's chat view still has references to services from the disposed window, causing an "InstantiationService has been disposed" error.

This occurs because the current implementation creates multiple instances that share the same underlying session data and services:

  • OpenChatSessionInNewWindowAction creates new editor instances but doesn't close the original
  • OpenChatSessionInSidebarAction loads sessions without closing editor instances
  • OpenChatSessionInNewEditorGroupAction duplicates sessions instead of moving them

Solution

Implement "move" behavior instead of "open in multiple places" to eliminate shared service references between windows.

Key Changes

Added Helper Methods

  • findAndCloseExistingSessionInstances(): Centralized method to close all existing instances of a session and preserve view state
  • closeEditorsBySessionId(): Safely closes all chat editors with matching session ID, extracting view state from active editors
  • clearWidgetBySessionId(): Clears chat widgets using proper clear() and waitForReady() pattern

Modified Actions to Use Move Behavior

All three chat session actions now:

  1. Close existing widget/editor instances before opening in new location
  2. Preserve user's view state (scroll position, selection) during the move
  3. Handle errors gracefully to prevent move operation failures

OpenChatSessionInNewWindowAction: Closes original instances before opening in auxiliary window
OpenChatSessionInSidebarAction: Closes existing editor instances before loading in sidebar
OpenChatSessionInNewEditorGroupAction: Clears original instances before opening in new editor group

Technical Implementation

The implementation follows existing patterns from chatMoveActions.ts and ensures:

  • Only one instance of a session exists at any time
  • View state is preserved during moves for better user experience
  • Proper cleanup prevents memory leaks
  • Error handling prevents cleanup failures from interrupting moves

Validation

Added comprehensive test suite in chatSessionActions.test.ts that verifies:

  • Widget instances are cleared before opening in new locations
  • All three actions implement the move behavior correctly
  • Proper service mocking ensures isolated testing

Result

This eliminates the disposal error by ensuring no shared service references exist between windows. When a user:

  1. Creates a new chat in the chat view ✅
  2. Right-clicks and selects "Open in New Window" ✅ (now moves instead of duplicates)
  3. Closes the new window ✅ (no more disposal errors)

The chat panel will remain clear after closing the auxiliary window since the session was moved rather than duplicated, preventing any disposal-related errors.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix chat session disposal error by implementing move behavior instead of duplicate opening Fix chat session disposal error by implementing move behavior instead of duplicate opening Sep 3, 2025
Copilot AI requested a review from osortega September 3, 2025 22:52
@osortega osortega changed the base branch from osortega/fix-arrow-function to main September 3, 2025 23:16
@osortega osortega closed this Sep 7, 2025
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat breaks when closing window with chat session

2 participants