Skip to content

fix: memory leak in lifeCycleMainService#315891

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

fix: memory leak in lifeCycleMainService#315891
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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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.
Copy link
Copy Markdown
Collaborator

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@deepak1556 deepak1556 enabled auto-merge (squash) May 12, 2026 07:39
@deepak1556 deepak1556 merged commit b8cefea into microsoft:main May 12, 2026
25 checks passed
@SimonSiefke SimonSiefke deleted the fix/memory-leak-lifecycle-main-service branch May 21, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants