Skip to content

fix: memory leak in mainThreadDocumentsAndEditors - #331170

Merged
Dmitriy Vasyura (dmitrivMS) merged 11 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-mainThreadDocumentsAndEditors
Aug 19, 2026
Merged

fix: memory leak in mainThreadDocumentsAndEditors#331170
Dmitriy Vasyura (dmitrivMS) merged 11 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-mainThreadDocumentsAndEditors

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

When restarting extensions, MainThreadDocumentsAndEditors left active MainThreadTextEditor wrappers subscribed to CodeEditorWidget events after their extension-host customer was disposed.

Change

The main change is just this one line of code, changing _textEditors from a Map to a DisposableMap:

private readonly _textEditors = new Map<string, MainThreadTextEditor>();

⬇️

private readonly _textEditors = this._register(new DisposableMap<string, MainThreadTextEditor>());

The other changes in the pr aren't necessarily needed. It's only to refactor the code a bit, using extends Disposable instead of this.toDispose.add.

Before

When restarting extensions 37 times, MainThreadTextEditor instances and their editor event callbacks grow:

extensions-restart-before-highlighted

After

No more matching MainThreadTextEditor callback growth is detected.

extensions-restart-after

Test Video

extensions-restart-7-runs.mp4
Copilot AI balanced review requested due to automatic review settings August 17, 2026 10:45

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR ensures MainThreadDocumentsAndEditors properly cleans up registered editor listeners by adopting the standard Disposable pattern, and adds a regression test to confirm editor option-change listeners are removed on dispose.

Changes:

  • Refactor MainThreadDocumentsAndEditors to extend Disposable and use _register(...) for lifecycle management.
  • Replace the internal editor map with a DisposableMap so tracked editors are disposed automatically.
  • Add a browser test asserting editor-property change listeners stop firing after disposal.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/vs/workbench/api/test/browser/mainThreadDocumentsAndEditors.test.ts Adds a regression test and wiring to count editor property-change notifications.
src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts Refactors disposal/lifecycle management to ensure editor listeners/resources are released.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/vs/workbench/api/test/browser/mainThreadDocumentsAndEditors.test.ts Outdated
Comment thread src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts
Comment thread src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts

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 does not compile...

@SimonSiefke

Copy link
Copy Markdown
Contributor Author
  • fixed tsc errors.
Comment thread src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts

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.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

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.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit b5f99ab into microsoft:main Aug 19, 2026
27 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.135.0 milestone Aug 19, 2026
Dileep Yavanmandha (dileepyavan) pushed a commit that referenced this pull request Aug 19, 2026
* fix: dispose main thread text editors

* simplify

* remove whitespace

* test: make editor listener assertion robust

* fix

* fix

* fix: missing removed editors id

* add unit test

* simplify code

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants