microsoft / microsoft/vscode

Agent Window retains a zsh process for every archived session terminal

Open
#329,316 3 comments 0 reactions 1 assignee Claimed by @anthonykim1 View on GitHub
agent-host bug terminal-process verification-found
Dominant language
TypeScript
Stars
193k
Forks
42.4k
PR merge metrics
PR metrics pending

Description

I don't think I understand the change in https://github.com/microsoft/vscode/pull/318985, seems that we now aggressively leak terminals.

---

## Type of Issue

Bug

## Reproduction

1. Open the Agent Window.
2. Activate several workspace-backed agent sessions. Each activation creates an Agent Host terminal for that session.
3. Mark those sessions as Done so their worktrees are removed.
4. Inspect the Agent Host process tree and open PTYs.

## Actual behavior

The terminal is moved to the background when its session is archived, but the Agent Host PTY and its interactive shell remain alive indefinitely. The worktree may be deleted while the shell still has it as its working directory.

In one live Insiders process after approximately 11 hours:

- 70 direct `/bin/zsh -il` children
- 70 `/dev/ptmx` handles retained by the Agent Host
- all 70 shells idle with no child processes
- 55 shells rooted in worktree paths
- 25 shells whose working directories had already been deleted
- about 32 MB aggregate shell RSS

Agent Host logs showed 71 terminal creations and only one terminal exit/disposal. Shell start times matched the `[TerminalManager] Creating terminal` log entries exactly.

## Cause

`SessionsTerminalContribution._onActiveSessionChanged` eagerly ensures a terminal whenever the active session changes. Its archive path calls `_hideTerminalsForSession`, which uses `moveToBackground` rather than disposing the terminal. This retention behavior is explicitly covered by the test `hides (does not dispose) terminals when session is archived`.

That policy conflicts with archive cleanup removing the session worktree, leaving a hidden interactive shell and PTY for every archived session.

Relevant history:

- [#297330](https://github.com/microsoft/vscode/pull/297330) introduced ensuring a terminal for every active session.
- [#318985](https://github.com/microsoft/vscode/pull/318985) intentionally changed archive cleanup from killing terminals to retaining them. This is the change most directly responsible for the retained shells.
- [#321506](https://github.com/microsoft/vscode/pull/321506) moved the current lifecycle to session-ID ownership.

## Expected behavior

Archived sessions should not accumulate live Agent Host shells after their worktrees are removed. Possible fixes include disposing terminals when archive removes the owned worktree, or creating session terminals lazily only when the terminal surface is used.

(Written by Copilot)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.