Skip to content

fix: memory leak in terminal disposal subscriptions - #340037

Merged
Dmitriy Vasyura (dmitrivMS) merged 7 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminal-disposalSubscriptions
Oct 11, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 7 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-terminal-disposalSubscriptions

Conversation

@SimonSiefke

@SimonSiefke Simon Siefke (SimonSiefke) commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Details

Each API terminal adds an onDisposed subscription to MainThreadTerminalService’s lifetime store. Closing the terminal removes its API mapping but leaves the subscription and launch configuration in that store.

Change

Keep the direct terminal-disposal listener and remove its subscription from the service store when the terminal closes. This preserves API mapping cleanup when a terminal is detached from its group before disposal. Also clear mappings for terminals closed during creation, and await queued TerminalService initialization in test setup so skipped GC tests leave no pending disposables.

Before

Creating and closing 37 API terminals with a missing shell adds 37 disposal callbacks. The service’s lifetime store grows from 21 to 58 entries.

before

After

The launch-request regression passes with exposed GC and fails when listener self-cleanup is reverted. Focused tests on Linux pass in WebKit (5 passed, 1 expected GC skip) and Chromium with exposed GC (6 passed), including detached-terminal and early-closure coverage. The selected module, including imported xterm tests, passes all 33 tests in Chromium. Focused ESLint passes.

Test Video

Original reproduction recording: seven failed terminal launches and closures. The missing-shell notification is the expected test trigger; a live Bash terminal remains.

test.mp4

AI disclosure: Model: GPT 6 Astra. Worktime: 48 min

Copilot AI balanced review requested due to automatic review settings October 6, 2026 12:23

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.

Copilot review overview

🟢 Approval recommended

The focused lifecycle change removes the leak while preserving terminal cleanup behavior with relevant regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes per-terminal disposal subscription growth in the terminal API service.

Changes:

  • Reuses the global terminal-disposal listener to remove extension-host mappings.
  • Adds lifecycle and memory regression tests.
File Description
mainThreadTerminalService.ts Centralizes API terminal cleanup.
mainThreadTerminalService.test.ts Tests disposal, collection, and live-terminal preservation.

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

@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 6, 2026
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

roblourens
roblourens previously approved these changes Oct 6, 2026

@dmitrivMS Dmitriy Vasyura (dmitrivMS) 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.

Memory leaks:

1) MainThreadTerminalService
       "after each" hook for "releases the launch request after an API terminal closes":
     
  @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/test/common/utils.js:45:24
  @http://localhost:49608/12011c733dde34e0200096e9061d4a61/test/unit/browser/renderer.html:56:27



2026-10-06T23:30:59.652Z pw:browser [pid=28574] <gracefully close start>
2026-10-06T23:30:59.852Z pw:browser [pid=28574] <process did exit: exitCode=0, signal=null>
2026-10-06T23:30:59.852Z pw:browser [pid=28574] starting temporary directories cleanup
The followings tests are failing:
2026-10-06T23:30:59.854Z pw:browser [pid=28574] finished temporary directories cleanup
 - MainThreadTerminalService "after each" hook for "releases the launch request after an API terminal closes" (reason: There are 2 undisposed disposables!
2026-10-06T23:30:59.854Z pw:browser [pid=28574] <gracefully close end>


==================== Leaking disposable 1/2: FunctionDisposable ====================
(shared with 1/2 leaks) at trackDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:51:40
(shared with 1/2 leaks) at trackDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:192:37
(shared with 1/2 leaks) at FunctionDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:256:20
(shared with 1/2 leaks) at toDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:271:32
(shared with 1/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/event.js:758:34
(shared with 1/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:370:53
(shared with 1/2 leaks) at Promise@[native code]
(shared with 1/2 leaks) at timeout@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:365:21
(shared with 1/2 leaks) at createCancelablePromise@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:14:28
    - stacktraces of 1 other leaks continue with createCancelablePromise@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:16:30
(shared with 2/2 leaks) at TerminalService@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/workbench/contrib/terminal/browser/terminalService.js:138:12
(shared with 2/2 leaks) at construct@[native code]
(shared with 2/2 leaks) at createInstance@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/platform/instantiation/common/instantiationService.js:93:36
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/workbench/api/test/browser/mainThreadTerminalService.test.js:56:62
(shared with 2/2 leaks) at callFn@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13313:27
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13301:13
(shared with 2/2 leaks) at next@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14689:15
(shared with 2/2 leaks) at cbHookRun@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14730:13
(shared with 2/2 leaks) at done@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13257:9
(shared with 2/2 leaks) at callFn@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13336:13
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13301:13
(shared with 2/2 leaks) at next@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14689:15
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14750:11
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/test/unit/browser/renderer.html:56:27
============================================================




==================== Leaking disposable 2/2: FunctionDisposable ====================
(shared with 1/2 leaks) at trackDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:51:40
(shared with 1/2 leaks) at trackDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:192:37
(shared with 1/2 leaks) at FunctionDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:256:20
(shared with 1/2 leaks) at toDisposable@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/lifecycle.js:271:32
(shared with 1/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/event.js:758:34
(shared with 1/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:17:62
(shared with 1/2 leaks) at Promise@[native code]
(shared with 1/2 leaks) at createCancelablePromise@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:16:30
    - stacktraces of 1 other leaks continue with createCancelablePromise@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/base/common/async.js:14:28
(shared with 2/2 leaks) at TerminalService@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/workbench/contrib/terminal/browser/terminalService.js:138:12
(shared with 2/2 leaks) at construct@[native code]
(shared with 2/2 leaks) at createInstance@http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/platform/instantiation/common/instantiationService.js:93:36
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/out/vs/workbench/api/test/browser/mainThreadTerminalService.test.js:56:62
(shared with 2/2 leaks) at callFn@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13313:27
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13301:13
(shared with 2/2 leaks) at next@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14689:15
(shared with 2/2 leaks) at cbHookRun@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14730:13
(shared with 2/2 leaks) at done@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13257:9
(shared with 2/2 leaks) at callFn@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13336:13
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:13301:13
(shared with 2/2 leaks) at next@http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14689:15
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/node_modules/mocha/mocha.js:14750:11
(shared with 2/2 leaks) at @http://localhost:49608/12011c733dde34e0200096e9061d4a61/test/unit/browser/renderer.html:56:27
============================================================

auto-merge was automatically disabled October 7, 2026 15:31

Head branch was pushed to by a user without write access

@SimonSiefke

Copy link
Copy Markdown
Contributor Author

Fixed another race condition in terminal disposal logic, should be good now.

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 new test imports another test module, unintentionally registering and running the unrelated xterm suite.

1 open finding

🧠 Review effort: Balanced

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 see comment

@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/test/browser/terminalInstance.test.ts
  • src/vs/workbench/contrib/terminal/test/browser/xterm/xtermTerminal.test.ts
  • src/vs/workbench/contrib/terminal/test/browser/xterm/xtermTestUtils.ts
  • src/vs/workbench/contrib/terminalContrib/links/test/browser/terminalLinkManager.test.ts

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 6133b42 into microsoft:main Oct 11, 2026
55 of 56 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.142.0 milestone Oct 11, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-terminal-disposalSubscriptions branch October 13, 2026 09:40
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.

9 participants