Skip to content

fix: memory leak in ipc.electron.ts#317846

Open
SimonSiefke wants to merge 2 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-electron-ipc
Open

fix: memory leak in ipc.electron.ts#317846
SimonSiefke wants to merge 2 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-electron-ipc

Conversation

@SimonSiefke
Copy link
Copy Markdown
Contributor

@SimonSiefke SimonSiefke commented May 21, 2026

Fixes a memory leak in ipc.electron.ts

Details

When a client connects, it is stored in Server.Clients. But it seems when a client disconnects, its entry from Server.Clients isn't removed.

const onDidClientReconnect = new Emitter<void>();
Server.Clients.set(id, toDisposable(() => onDidClientReconnect.fire()));

Change

The changes tries to ensure when a client disconnects, its entry from Server.Clients is removed as well.

const onDidClientReconnect = new Emitter<void>();
Server.Clients.set(id, toDisposable(() => onDidClientReconnect.fire()));

/* Ensure to remove entry from Server.Clients on disconnect  */
Event.once(onDidClientDisconnect)(() => {
	if (Server.Clients.get(id) === reconnectDisposable) {
		Server.Clients.delete(id);
	}
	onDidClientReconnect.dispose();
});

Before

When opening and closing a new window 17 times, the number of functions in ipc.electron.ts seems to grow by one each time:

window-open-new 0

After

When opening and closing a new window 17 times, no more leak in ipc.electron.ts is detected (if you look closely at the charts there is one less row):

window-open-new 5

Copilot AI review requested due to automatic review settings May 21, 2026 20:31
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

Note

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

This PR improves lifecycle management for Electron IPC clients by making reconnection handling explicit and ensuring per-client resources are cleaned up when a client disconnects.

Changes:

  • Extracted the reconnection IDisposable into a named constant for safer identity checks.
  • Added one-time cleanup on client disconnect/reconnect to remove stale Server.Clients entries and dispose the reconnect emitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants