Skip to content

fix: memory leak in terminal auto replies - #336022

Merged
Megan Rogge (meganrogge) merged 6 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminalAutoReplies-config
Oct 30, 2026
Merged

Megan Rogge (meganrogge) merged 6 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminalAutoReplies-config

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

Removing terminal auto-replies disposes the current responders but leaves the old prompts in the pty host's configuration map. Opening another terminal installs those removed replies again.

Change

Clear the saved auto-replies when uninstalling all replies.

Before

When adding and removing an auto-reply and opening a terminal 37 times, 37 extra responders and their data listeners remain:

before

After

No more growth in terminal auto-reply responders or their data listeners is detected.

Test Video

video.webm

Copilot AI balanced review requested due to automatic review settings September 13, 2026 09:08
@vs-code-engineering

vs-code-engineering Bot commented Sep 13, 2026 •

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/platform/terminal/common/terminal.ts
  • src/vs/platform/terminal/node/ptyHostService.ts
  • src/vs/platform/terminal/node/ptyService.ts
  • src/vs/platform/terminal/node/terminalContrib/autoReplies/autoRepliesContribController.ts
  • src/vs/workbench/contrib/terminal/browser/remoteTerminalBackend.ts
  • src/vs/workbench/contrib/terminal/common/remote/remoteTerminalChannel.ts
  • src/vs/workbench/contrib/terminal/electron-browser/localTerminalBackend.ts
  • src/vs/workbench/contrib/terminalContrib/autoReplies/browser/terminal.autoReplies.contribution.ts
  • src/vs/workbench/contrib/terminalContrib/autoReplies/test/browser/terminalAutoRepliesContribution.test.ts

@dmitrivMS Dmitriy Vasyura (dmitrivMS) added freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues terminal General terminal issues that don't fall under another label labels Sep 24, 2026
processAutoResponders.delete(match);
}
}
this._autoReplies.clear();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI Review: The saved _autoReplies map is shared by every window using the same pty host. uninstallAllAutoReplies() carries no owner, and each window's TerminalAutoRepliesContribution runs "uninstall all, then install my own config" when its setting changes. With this clear(), a change in one window now also removes other windows' still-configured replies from the map that handleProcessReady uses for new terminals.

Scenario (traced from source, not reproduced):

  1. Window A has workspace settings "terminal.integrated.autoReplies": { "Terminate batch job (Y/N)": "Y\r" } and runs a batch-file task or watch task.
  2. Window B is another trusted workspace, open at the same time, with different workspace-level auto replies.
  3. Edit only B's workspace terminal.integrated.autoReplies. Only B's configuration listener fires. It uninstalls globally and reinstalls B's replies only.
  4. In A, a task starts in a new terminal, or you open one and run the script, then interrupt it.

Expected: A's reply still answers Terminate batch job (Y/N)?, as it did in new terminals before this change.
Actual with this PR: the new terminal gets only B's replies and waits at the prompt. Nothing is logged, and A stays this way until its own setting changes or the window reloads. An unattended task in A can hang.

A user-settings change can likely trigger this too. Every window handles it and runs its own uninstall-all/install, and whichever window runs last wins, so A's workspace-only entries can be dropped even though nothing was edited in B.

Before this PR, A's existing terminals already lost their responders in this case. The regression is specific to terminals created afterwards. Removing the clear() would bring the leak back, so that isn't the fix. Possible directions:

  • Scope auto replies by owner (for example, a window/client id passed through ITerminalBackend.installAutoReply/uninstallAllAutoReplies and the pty host IPC). Then an uninstall only removes that owner's entries and responders.
  • Or accept the global semantics explicitly and track per-window scoping as a follow-up.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P2 - still present after the main merge

Rechecked against 03992378c14ce23f0cc2c006a6e3aa89a169c5ff: the merge preserves the repeated-ready disposal fix, but does not address the configuration ownership problem in this thread.

A minimal case needs no concurrent updates: trusted windows A and B have different workspace replies; only B changes its replies to {}; A then opens a new terminal. B clears the shared map and installs nothing. A receives no configuration change, so its still-configured prompt is absent when the new terminal becomes ready. A task waiting for that reply can remain blocked. The per-window configuration listener still performs global uninstall followed by only that window's configuration.

The regression remains specifically new terminals: existing terminals already lost responders before this PR, but new terminals previously reinstalled A's saved entry. Please make replacement/removal preserve other live owners' still-configured replies and add two-owner regression coverage. Removing clear() alone would restore the stale-entry bug.

Both independent reviews confirmed this source path on the current head. This is source-traced, not runtime-reproduced; Code OSS testing was not run.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems to be introducing a regression (see other comment)

# Conflicts:
#	src/vs/platform/terminal/test/node/autoRepliesContribController.test.ts
} finally {
contribution.handleProcessDispose(1);
// Let the response throttle finish before checking disposable ownership.
await timeout(1100);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P3 (non-blocking): use fake timers for both generated test cases

Each case unconditionally waits 1,100 ms here to drain the responder's one-second throttle, adding two real-time waits (about 2.2 seconds total) to every run of this suite.

Please wrap each generated test in runWithFakedTimers({ useFakeTimers: true }, async () => { ... }), matching the immediately following repeated-ready test. The existing await timeout(1100) can remain inside that wrapper so the throttle finishes before disposable checks, but in virtual time rather than slowing the suite down.

@SimonSiefke
Simon Siefke (SimonSiefke) marked this pull request as draft September 28, 2026 11:48
Carry a workbench owner ID through local and remote pty calls. Remove only that owner's saved entries and responders, restoring another owner's reply for shared prompts. Clean up the owner's configuration when its contribution is disposed.

Add two-owner removal and replacement coverage, shared-prompt fallback and IPC ownership tests. Use fake timers for the auto reply regression tests.
Workbench contributions are disposed after the remote management connection closes. Join owner cleanup during onWillShutdown so it completes before the Last remote disconnect joiner, and stop configuration updates while cleanup is pending.

Cover delayed cleanup for initially registered and late backends, repeated owner reloads, preservation of a live peer, and configuration changes during shutdown.

@meganrogge Megan Rogge (meganrogge) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@meganrogge
Megan Rogge (meganrogge) merged commit 3516bd0 into microsoft:main Oct 30, 2026
55 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.145.0 milestone Oct 30, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-terminalAutoReplies-config branch October 30, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues terminal General terminal issues that don't fall under another label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants