Skip to content

sessions: fix terminals leaked by chat windows - #346125

Merged
Sandeep Somavarapu (sandy081) merged 7 commits into
mainfrom
agents/chat-window-terminal-leak
Nov 4, 2026
Merged

Sandeep Somavarapu (sandy081) merged 7 commits into
mainfrom
agents/chat-window-terminal-leak

Conversation

@sandy081

Copy link
Copy Markdown
Member

Fixes terminals leaking from the chat windows of the Agents window: shells left running without a terminal, duplicated terminals, terminals killed or left behind when a window closes, and terminals of a closed or deleted session that no session cleans up. Follow-up to #345683 and #346039.

One commit per leak, each with a test that fails without its fix.

Leaks and fixes

Numbers are from live runs of Code - OSS built from this branch (and from main for "before"), in an isolated profile with local Agent Host sessions: shells are the shell processes under the Code - OSS process tree, terminals are the terminal service's instances.

S3: reload leaves the chat window's terminals running as orphan shells

  • Repro: a chat window with its terminals showing → Developer: Reload Window, twice.
  • Before: each reload left one more Agent Host shell per chat window terminal running with no terminal (5 shells for 3 terminals after one reload, 6 for 3 after two, alive until quit). The window showed a new terminal each time.
  • Cause: on reload, TerminalEditorInput detached the process of every editor terminal, whether or not it persists. Nothing reattaches to a non-persistent one, such as an Agent Host terminal, and the Agent Host does not reap it.
  • Fix: detach on reload only terminals that persist (instance.shouldPersist), as TerminalService already does for the panel; others are disposed, as in the panel.
  • After: after each of two reloads, shells = terminals (2 = 2), also 90 s later; the old shells ended.

Note

This changes the shared TerminalEditorInput, in every window: a terminal editor whose terminal does not persist, such as an Agent Host terminal or a non-persistent terminal open as an editor in a normal window, is now disposed on reload instead of detached. Persistent local pty editor terminals still detach on reload and reattach as their editors are restored. The new terminalEditorInput.test.ts covers both.

L1: a task rerun after hiding the terminals creates a new task terminal each time

  • Repro: in a chat window, run a task → hide the terminals (Cmd+J) → run it again; repeat.
  • Before: one more "Task: …" terminal and shell per hide + rerun (3 → 4 → 5 shells), all piling up in the window's terminals.
  • Cause: the task runner reused a task terminal only while it was in the foreground; hiding moves the chat window's terminals to the panel's background.
  • Fix: also reuse a task terminal that the active chat window of the session holds while its terminals hide; it shows there again.
  • After: run → hide → run → hide + run: 3 shells throughout; the hidden task terminal was reused.

L2: deleting a session leaves its focused chat window terminal running

  • Repro: a session in a chat window, its terminal focused → delete the session.
  • Before: "Skipping active terminal … (user is working in it)"; the window closed, the terminal went to the background with no session, its shell still running, reachable only through the developer "Show All Terminals" command.
  • Cause: removal spares the active terminal, for the untitled → committed graduation, including one in a chat window or in the background.
  • Fix: spare the active terminal only while it shows in the main window.
  • After: covered by a unit test (not re-checked live).

L3: terminals a chat window takes are not tracked for its session

  • Repro: in a chat window, run a task, or open a terminal with New Terminal / Split → archive the session.
  • Before: the task terminal survived the archive in the background (its shell running), and later surfaced in, and was reused by, another session with the same working directory.
  • Cause: task terminals and New/Split terminals of a chat window were never tracked for the window's session.
  • Fix: the terminals a chat window takes are tracked for the session it shows, so archive/delete clean them up and other sessions do not take them. A terminal that enters a chat window's parts, such as one the user drags there from another session, is re-assigned to that window's session.
  • After: covered by unit tests (not re-checked live).

S2: Close Chat Window / Move to Main Window kill the terminals beside the chat

  • Repro: drag a terminal tab from a chat window's terminals next to the chat → Cmd+W, or Move to Main Window.
  • Before: the terminal beside the chat was killed (Cmd+W: 4 → 3 shells; Move to Main Window: 3 → 2 or 4 → 3), and Move to Main Window did not bring it back (Agent Host terminals cannot be serialized). Closing the OS window released them instead: the two close paths differed.
  • Cause: ChatWindow.close() closed the main and chat parts before the window's terminals were released, closing the terminal editors there. In addition, moving an inactive terminal editor between groups makes the terminal editor service forget its input, so releasing the terminal did not detach that editor.
  • Fix: new IChatWindow.onWillCloseParts, fired by close() once nothing keeps the window open, before its main and chat parts close. Terminals in the window's parts other than its terminals part, such as beside the chat, in its main part or in parts it created, go to the panel's background then. Releasing a terminal detaches and closes its editor input directly, so that the editor neither ends the terminal as its part closes nor stays as a blank tab. (That works around the terminal editor service forgetting the input of a terminal editor that moves between groups until it shows; tracked separately.) On the OS close path, the release before the parts close now also covers all parts. Terminal editors are no longer serialized to be reopened by Move to Main Window / drag-out reopen, since they leave the window.
  • After: Cmd+W with 3 terminals, one beside the chat: 4 → 4 shells, all in the panel's background. Move to Main Window with one beside the chat: 4 → 4 shells, the terminals show in the main window.

Known gap: on a drag-out reopen, a persistent local pty terminal beside the chat can still be revived from the captured chat-part state, while the terminal also went to the panel: two views of one process. Agent Host terminals are not serializable and are not affected.

S4: a chat window opens with only one of its session's terminals

  • Repro: a chat window with 2+ terminals → close it, then open the chat in a new window again (or reopen by dragging the chat out) and show its terminals.
  • Before: only one terminal came back; the others stayed hidden in the background (confirmed by the user on Insiders).
  • Fix: a chat window shows all of its session's terminals that no other chat window shows, in the order and with the active one they last showed with. While the main window shows the same session, its panel keeps all but one. If the window hides its terminals or closes while they move in, the rest stay with their session in the panel. The remembered order goes away with a removed or archived session.
  • After: all 3 terminals came back in their last order, with the last active one active.

Not in this PR

"View: Move Editor into New Window" on the chat of a chat window closes the window without reopening the chat. It reproduces identically on main (e80d09a), so it is pre-existing.

Validation

  • set -o pipefail; pnpm run typecheck-client: passed (also tsc --noEmit -p src/tsconfig.json on each intermediate commit).
  • pnpm run build-fast --client-only: passed.
  • ./scripts/test.sh --run src/vs/sessions/contrib/terminal/test/browser/chatWindowTerminals.test.ts --run src/vs/sessions/contrib/terminal/test/browser/sessionsTerminalContribution.test.ts --run src/vs/sessions/contrib/terminal/test/browser/agentHostSessionTaskRunner.test.ts --run src/vs/workbench/contrib/chatWindow/test/browser/chatWindow.test.ts --run src/vs/workbench/contrib/chatWindow/test/browser/chatWindowMoveEditors.test.ts --run src/vs/workbench/contrib/terminal/test/browser/terminalEditorInput.test.ts: 189 passing, 0 failing.
  • The same tests with each fix's tests over the previous commit's sources: the new tests of each fix fail (S4's through the source API rename).
  • pnpm run eslint on the 12 changed files: passed.
  • Live, in Code - OSS built from this branch, with the before/after numbers above: S3, L1, S2 (both close paths) and S4. L2 and L3 were confirmed as leaks live on main; their fixes are covered by unit tests.

… does

On reload, a terminal editor detached its process whether or not the terminal
persists. For a terminal that does not persist, such as an Agent Host
terminal, nothing reattaches: each reload of an Agents chat window left the
shells of its terminals running without a terminal. Detach only terminals
that persist, as the terminal service does for the panel's terminals.

This changes the shared TerminalEditorInput, in every window: a terminal
editor whose terminal does not persist, such as an Agent Host or a
non-persistent terminal open as an editor in a normal window, is now disposed
on reload instead of detached. Persistent local pty editor terminals still
detach on reload and reattach as their editors are restored; the test covers
both.

Co-authored-by: Copilot <[email protected]>
…ls hide

Hiding a chat window's terminals moves them to the panel's background, and the
task runner reused only foreground terminals: each hide and rerun created
another task terminal, and the old ones piled up in the window. Also reuse a
task terminal that the active chat window of the session holds; it shows
there again.

Co-authored-by: Copilot <[email protected]>
Removal spares the active terminal, for the untitled to committed graduation.
A chat window's terminal of the removed session, or one its closing window put
in the background, was spared too, and kept running with no session and
nowhere to show. Only spare the active terminal while it shows in the main
window.

Co-authored-by: Copilot <[email protected]>
Task terminals and terminals opened by New Terminal or Split in a chat window
were never tracked for the window's session: archiving or removing the session
left them running, and they surfaced in the panel of another session with the
same working directory. Track the terminals a chat window takes for the
session it shows.

Co-authored-by: Copilot <[email protected]>
…w closes

Close Chat Window and Move to Main Window closed the main and chat parts before
releasing the window's terminals, which killed terminals beside the chat, in
the main part or in parts the window created; closing the OS window released
them first. Fire onWillCloseParts once nothing keeps the window open, before
those parts close, and release the terminals of all the window's parts to the
panel, so that both close paths keep them running. Terminals are no longer
serialized to be opened again, as they now leave the window.

Co-authored-by: Copilot <[email protected]>
A chat window opening for a session, such as again after the chat moved out,
or restored, showed only one terminal of the session, and the others stayed
hidden in the background. Show all the session's terminals that no other chat
window shows, in the order and with the active one they last showed with.
While the main window shows the session, its panel keeps all but one.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 4, 2026 21:06
@sandy081 Sandeep Somavarapu (sandy081) added this to the 1.146.0 milestone Nov 4, 2026
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

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

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminal/browser/terminalEditorInput.ts
  • src/vs/workbench/contrib/terminal/test/browser/terminalEditorInput.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.

🟡 Changes recommended

Adopted terminals can be reused on the wrong host or in the wrong directory, incomplete ownership tracking risks unintended termination or missed cleanup, and non-default editor positioning reverses restored terminal order.

3 open issues
What changed in this PR

This PR addresses terminal leaks and lifecycle inconsistencies in Agents chat windows, following #345683 and #346039. It also updates shared terminal-editor reload handling to dispose terminals that cannot persist.

Changes:

  • Track session ownership and reuse hidden task terminals for session cleanup.
  • Release terminals before chat-window parts close and restore multiple terminals with their remembered order and active selection.
  • Add regression tests for reload, cleanup, reuse, and window-close behavior.
File Description
src/​vs/​workbench/​contrib/​terminal/​test/​browser/​terminalEditorInput.test.ts Tests persistent and non-persistent terminal reload behavior.
src/​vs/​workbench/​contrib/​terminal/​browser/​terminalEditorInput.ts Detaches only persistent terminals on reload.
src/​vs/​workbench/​contrib/​chatWindow/​test/​browser/​chatWindow.test.ts Tests notification before window parts close.
src/​vs/​workbench/​contrib/​chatWindow/​browser/​chatWindowService.ts Fires the new pre-close notification.
src/​vs/​workbench/​contrib/​chatWindow/​browser/​chatWindowEditors.ts Excludes released terminals from close-time serialization.
src/​vs/​workbench/​contrib/​chatWindow/​browser/​chatWindow.ts Defines the pre-close event contract.
src/​vs/​sessions/​contrib/​terminal/​test/​browser/​sessionsTerminalContribution.test.ts Tests terminal ownership, cleanup, and multi-terminal selection.
src/​vs/​sessions/​contrib/​terminal/​test/​browser/​chatWindowTerminals.test.ts Tests release, restoration, ordering, and interrupted moves.
src/​vs/​sessions/​contrib/​terminal/​test/​browser/​agentHostSessionTaskRunner.test.ts Tests reuse of hidden chat-window task terminals.
src/​vs/​sessions/​contrib/​terminal/​browser/​sessionsTerminalContribution.ts Associates terminals with sessions and adjusts cleanup and selection.
src/​vs/​sessions/​contrib/​terminal/​browser/​chatWindowTerminals.ts Coordinates terminal ownership, release, and restoration.
src/​vs/​sessions/​contrib/​terminal/​browser/​agentHostSessionTaskRunner.ts Allows reuse of task terminals held by the active chat window.

Review effort: Balanced

- A terminal a chat window adopts from another session is tracked for the
  window's session without caching that session's scope, so matching checks
  its actual working directory and host.
- A chat window's terminals keep their remembered order also where new editors
  open first or to the left: their order is enforced once they opened.
- A terminal entering any part of a chat window, such as next to the chat or in
  its main part, is the window's and its session's, not only one entering the
  part that holds its terminals.

Co-authored-by: Copilot <[email protected]>
@sandy081
Sandeep Somavarapu (sandy081) merged commit a2c722f into main Nov 4, 2026
59 checks passed
@sandy081
Sandeep Somavarapu (sandy081) deleted the agents/chat-window-terminal-leak branch November 4, 2026 21:35
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.

4 participants