Repository navigation
sessions: fix terminals leaked by chat windows - #346125
Merged
Sandeep Somavarapu (sandy081) merged 7 commits intoNov 4, 2026
Merged
Conversation
… 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]>
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: Anthony Kim (@anthonykim1)Matched files:
|
Dirk Bäumer (dbaeumer)
previously approved these changes
Nov 4, 2026
Contributor
There was a problem hiding this comment.
🟡 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]>
roblourens
approved these changes
Nov 4, 2026
Sandeep Somavarapu (sandy081)
deleted the
agents/chat-window-terminal-leak
branch
November 4, 2026 21:35
This was referenced Nov 5, 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.


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
mainfor "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
TerminalEditorInputdetached 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.instance.shouldPersist), asTerminalServicealready does for the panel; others are disposed, as in the panel.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 newterminalEditorInput.test.tscovers both.L1: a task rerun after hiding the terminals creates a new task terminal each time
L2: deleting a session leaves its focused chat window terminal running
L3: terminals a chat window takes are not tracked for its session
S2: Close Chat Window / Move to Main Window kill the terminals beside the chat
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.IChatWindow.onWillCloseParts, fired byclose()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.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
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 (alsotsc --noEmit -p src/tsconfig.jsonon 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.pnpm run eslinton the 12 changed files: passed.main; their fixes are covered by unit tests.