Skip to content

fix: memory leak in lifeCycleMainService - #315891

Merged
Robo (deepak1556) merged 2 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-lifecycle-main-service
May 12, 2026
Merged

Robo (deepak1556) merged 2 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-lifecycle-main-service

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Fixes a memory leak in lifeCycleMainService.

Details

It seems the way the event listener is setup, when an okChannel event is received, the listener for the cancelChannel is not removed and vice versa.

validatedIpcMain.once(okChannel, () => {
  /* 
   * this can run, but if we get into this branch, 
   * we never remove the cancelChannel listener 
   * from below and vice versa
   */
  resolve(false); // no veto 
});

validatedIpcMain.once(cancelChannel, () => {
  resolve(true); // veto
});

Change

The change tries to ensure that always both listeners are removed when either a okChannel event or a cancelChannel event is received.

const cleanup = (value: boolean) => {
  /* Always ensure to clean up both listeners */
  validatedIpcMain.removeListener(okChannel, okListener);
  validatedIpcMain.removeListener(cancelChannel, cancelListener);
  resolve(value);
};

const okListener = () => {
  cleanup(false); // no veto
};

const cancelListener = () => {
  cleanup(true); // veto
};

validatedIpcMain.on(okChannel, okListener);
validatedIpcMain.on(cancelChannel, cancelListener);

Before

When opening and closing a new window 17 times, the number of CodeWindow instances and some other functions seems to grow by 1 each time:

codewindow

After

When opening and closing a new window 17 times, no more CodeWindow leak is detected. And overall less functions seem to be leaked:

window-open-new-code-window

Copilot AI review requested due to automatic review settings May 11, 2026 22:25

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

This PR fixes a listener leak in the Electron main-process LifecycleMainService during the onBeforeUnload handshake with the renderer by ensuring both IPC listeners are always removed when either the “ok” or “cancel” reply arrives.

Changes:

  • Replaces two independent validatedIpcMain.once(...) registrations with removable validatedIpcMain.on(...) listeners.
  • Adds a shared cleanup function that removes both listeners and resolves the veto promise from either branch.

@deepak1556 Robo (deepak1556) 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.

Thanks!

@deepak1556
Robo (deepak1556) enabled auto-merge (squash) May 12, 2026 07:39
@deepak1556
Robo (deepak1556) merged commit b8cefea into microsoft:main May 12, 2026
25 checks passed
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-lifecycle-main-service branch May 21, 2026 15:01
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jun 26, 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.

4 participants