Skip to content

fix: detach terminal processes on window close when other windows are open (#304649) - #307472

Open
maruthang wants to merge 2 commits into
microsoft:mainfrom
maruthang:fix/issue-304649-terminal-window-close
Open

fix: detach terminal processes on window close when other windows are open (#304649)#307472
maruthang wants to merge 2 commits into
microsoft:mainfrom
maruthang:fix/issue-304649-terminal-window-close

Conversation

@maruthang

@maruthang maruthang commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #304649

When multiple VS Code windows are open and one is closed, the closing window's _onWillShutdown unconditionally calls instance.dispose() on its terminal instances, which sends a kill signal to the PTY processes via the shared pty host. This kills terminal sessions that other windows are still connected to.

Changes

In terminalService.ts's _onWillShutdown, added a check: when _shutdownWindowCount > 1 (other windows are still open), call instance.detachProcessAndDispose() instead of instance.dispose(). This tears down the local IPC connection without killing the PTY process, so the process stays alive for the remaining windows.

How to test

  1. Open two VS Code windows
  2. Start a long-running program in Window 1's terminal (e.g., ping localhost)
  3. Close Window 2
  4. Verify Window 1's terminal is unaffected — no "Restarting the terminal because the connection to the shell process was lost..." message

@maruthang
maruthang force-pushed the fix/issue-304649-terminal-window-close branch 2 times, most recently from eb3eb83 to d7decd1 Compare April 6, 2026 10:53
@maruthang
maruthang force-pushed the fix/issue-304649-terminal-window-close branch from d7decd1 to bc22791 Compare April 6, 2026 10:58
Copilot AI review requested due to automatic review settings July 16, 2026 07:47
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@anthonykim1

Matched files:

  • src/vs/workbench/contrib/terminal/browser/terminalService.ts
  • src/vs/workbench/contrib/terminal/test/browser/terminalService.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates terminal shutdown behavior to avoid immediately killing PTY processes shared across windows.

Changes:

  • Detaches terminal processes when other windows remain open.
  • Adds regression tests for shutdown window-count scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
terminalService.ts Adjusts terminal cleanup during shutdown.
terminalService.test.ts Tests detach/dispose decisions by window count.

instance.detachProcessAndDispose(TerminalExitReason.Shutdown);
} else if (hasOtherWindowsOpen) {
// Detach without killing the process so other windows can continue using it
instance.detachProcessAndDispose(TerminalExitReason.Shutdown);
// When other VS Code windows are still open, only detach from the terminal processes instead
// of shutting them down. This prevents the closing window from killing PTY processes that
// other windows are still connected to via the shared pty host.
const hasOtherWindowsOpen = this._shutdownWindowCount !== undefined && this._shutdownWindowCount > 1;
Comment on lines +717 to +719
} else if (hasOtherWindowsOpen) {
// Detach without killing the process so other windows can continue using it
instance.detachProcessAndDispose(TerminalExitReason.Shutdown);
Comment on lines +242 to +244
test('should detach (not dispose) when window count is unknown but multiple windows are open', () => {
// When _shutdownWindowCount is undefined (native delegate not set), default to safe behaviour.
// This test documents that the guard only fires when the count is explicitly > 1.
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.

Closing A VSCode Window Causes Another Window to Lose Connection to Shell Process

3 participants