Skip to content

fix: memory leak in utilityProcessWorkerMainService - #294005

Merged
Dmitriy Vasyura (dmitrivMS) merged 13 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-utility-process
May 7, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 13 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-utility-process

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Fixes a memory leak in utilityProcessWorkerMainService.

Details

In utilityProcessWorkerMainService, when a worker is terminated, it is removed from the this.workers map. But the worker itself seems be not be disposed.

Event.once(worker.onDidTerminate)(reason => {
    /* ... */
	this.workers.delete(workerId);
	onDidTerminate.complete({ reason });
});

Change

The change uses a DisposableMap so that the worker is be deleted from the map and also gets disposed.

Event.once(worker.onDidTerminate)(reason => {
	/* ... */
	this.workers.deleteAndDispose(workerId);
	onDidTerminate.complete({ reason });
});

Another related (but not really important) change is removing some cleanup code from disposeWorker which seems to be already handled by worker.onDidTerminate.

worker.kill();
 ̶w̶o̶r̶k̶e̶r̶.̶d̶i̶s̶p̶o̶s̶e̶(̶)̶;̶
̶t̶h̶i̶s̶.̶w̶o̶r̶k̶e̶r̶s̶.̶d̶e̶l̶e̶t̶e̶(̶w̶o̶r̶k̶e̶r̶I̶d̶)̶;̶

Before

shapes at 26-02-09 21 12 07

When opening and closing a window 17 times, the number of various utility process functions seems to grow each time.

After

When opening and closing a window 17 times, the number of utility process functions stays constant:

window-open-new-10

Copilot AI review requested due to automatic review settings February 9, 2026 20:26
@vs-code-engineering

vs-code-engineering Bot commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Benjamin Pasero (@bpasero)

Matched files:

  • src/vs/platform/utilityProcess/electron-main/utilityProcessWorkerMainService.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.

Pull request overview

Fixes a memory leak in the Electron main-process UtilityProcessWorkerMainService by ensuring worker instances are disposed when their underlying utility process terminates, rather than only removing them from the tracking map.

Changes:

  • Replace the internal Map of workers with a registered DisposableMap to manage worker lifecycles.
  • Dispose workers on termination via deleteAndDispose in the termination handler.
  • Simplify disposeWorker to only request termination (kill()), relying on the termination pathway for cleanup.

If deleteAndDispose throws, the deferred promise would never complete, leaving awaiters hanging. Reorder so the promise completes first.

Co-authored-by: Copilot <[email protected]>
disposeWorker -> kill() is async, so a previous worker for the same id can terminate after a new worker has already taken its slot in the map. Guard the deletion with an identity check and dispose the worker directly.

Co-authored-by: Copilot <[email protected]>
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) enabled auto-merge (squash) May 3, 2026 21:01
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 98cb242 into microsoft:main May 7, 2026
26 checks passed
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-utility-process branch May 8, 2026 11:45
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jun 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants