Skip to content

fix: memory leak in native context menu dismissal - #338214

Merged
Dmitriy Vasyura (dmitrivMS) merged 5 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-nativeContextMenu-dismissal
Oct 5, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 5 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-nativeContextMenu-dismissal

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

Dismissing a native context menu leaves its one-shot click listener and menu actions in the renderer. Removing the callback through the context bridge does not remove the original listener.

Change

Complete the one-shot click channel when the native menu closes, using a no-selection value that the renderer safely ignores. Selected actions still run normally.

Before

Opening an editor tab’s native context menu and dismissing it with Escape 37 times grows per-menu click subscriptions from 5 to 42 and adds 888 MenuItemAction objects.

before

After

No more leak detected.

Test Video

Seven native context-menu open and Escape dismissal cycles.

test.mp4

AI disclosure: Model: GPT 6 Astra. Worktime: 46 min

Copilot AI balanced review requested due to automatic review settings September 27, 2026 17:34

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.

Copilot review overview

🟡 Changes recommended

The test fixture’s file URL conversion is broken on Windows.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes native context-menu dismissal leaks by completing the renderer’s one-shot click channel.

Changes:

  • Sends a no-selection sentinel when menus close.
  • Safely ignores that sentinel in the renderer.
  • Adds native Electron lifecycle regression tests.
File Description
electron-main/​contextmenu.ts Completes click IPC on close.
electron-browser/​contextmenu.ts Ignores sentinel values.
test/​electron-main/​contextmenu.test.ts Runs lifecycle scenarios.
test/​electron-main/​fixtures/​contextmenu.ts Implements native menu fixture.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Windows regression in the new native-menu test fixture

The protocol handler in fixtures/contextmenu.ts:38 passes a decoded URL pathname to pathToFileURL() as though it were a native filesystem path.

On Windows, the renderer URL constructed at lines 63-64 has a pathname like /C:/repo/out/.../contextmenu.js. Passing that directly to pathToFileURL() resolves it to file:///C:/C:/repo/out/.../contextmenu.js, rather than the intended file URL. Consequently, net.fetch() cannot load the renderer module and all four new scenarios are expected to fail before reaching their menu assertions.

Please preserve file-URL escaping and Windows drive semantics when mapping the custom protocol back to a file URL, rather than treating the URL pathname as a filesystem path.

I confirmed the transformation with a deterministic Windows Node v24.18.0 round-trip probe: the intended file exists, the fixture-converted path does not, and the standard fileURLToPath() / pathToFileURL() control succeeds. This is an expression-level reproduction; I did not run the full Electron fixture.

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.

See comment please

@SimonSiefke

Copy link
Copy Markdown
Contributor Author

Removed new (ai generated) e2e tests because they seemed weird, complicated and potentially slow.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit a8fe268 into microsoft:main Oct 5, 2026
35 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.142.0 milestone Oct 5, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-nativeContextMenu-dismissal branch October 5, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants