fix: memory leak in terminal process startup by SimonSiefke · Pull Request #340582 · microsoft/vscode · GitHub
Skip to content

fix: memory leak in terminal process startup - #340582

Merged
Dmitriy Vasyura (dmitrivMS) merged 6 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminalProcessStartupDisposal
Oct 13, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 6 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminalProcessStartupDisposal

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

A terminal can be shut down while executable validation or spawn throttling is still pending. Startup then creates a PTY and title-polling timer after disposal, so that timer is never cleared.

Change

Check whether the terminal has been disposed after the final await and return before spawning the PTY.

Before

When disposing terminals during startup 37 times, disposed terminal instances and title-polling callbacks grow by 37:

before

After

No more leak detected.

Test Video

test-video.webm

AI disclosure: Model: GPT-6.1-Sol. Worktime: 26 min

Copilot AI balanced review requested due to automatic review settings October 8, 2026 21:02
@vs-code-engineering

Copy link
Copy Markdown
Contributor

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

The Windows-only spawn-throttling disposal path lacks deterministic test coverage.

1 open finding
What changed in this PR

Prevents PTY and title-timer creation after terminal disposal during asynchronous startup.

Changes:

  • Adds a post-throttle disposal guard.
  • Adds startup disposal and launch validation tests.
File Description
terminalProcess.ts Stops startup after disposal.
terminalProcess.test.ts Tests startup and disposal behavior.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/vs/platform/terminal/test/node/terminalProcess.test.ts Outdated
@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 and removed triage-needed labels Oct 9, 2026
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Windows shutdown-during-validation test can fail before disposal

At the current head, 7a668852917fcd466b603c0c8e832a64fcfaf2b2, the newly Windows-enabled test at lines 81-87 assumes that shutdown(true) prevents startup from reaching spawn.

However, immediate shutdown is explicitly restricted to non-Windows. On Windows, shutdown instead schedules the 250-ms data-flush timer. In an isolated run, the first spawn-throttle call has no previous spawn/kill operation to wait for. If validation completes before that shutdown timer, the terminal is still undisposed at the new guard, so startup reaches the pre-spawn trace and increments spawnAttempts to 1. The assertion at line 87 expects 0.

The fixture counts that trace; it does not stub native PTY creation, so this path can also launch a real child. This is a substantive test regression from enabling the suite on Windows, not evidence that the production disposal guard is incorrect.

Please make this test deterministic by holding startup until shutdown/disposal has completed before releasing it. Alternatively, keep this immediate-shutdown-dependent test skipped on Windows while retaining the new deterministic throttle-disposal test there.

Validation: source-traced at the pinned head; I have not executed this test on Windows.

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.

Please consider the comment.

@SimonSiefke
Simon Siefke (SimonSiefke) marked this pull request as draft October 10, 2026 19:33
@SimonSiefke

Simon Siefke (SimonSiefke) commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@SimonSiefke
Simon Siefke (SimonSiefke) marked this pull request as ready for review October 13, 2026 13:59
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 9603443 into microsoft:main Oct 13, 2026
35 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.143.0 milestone Oct 13, 2026
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