Skip to content

Don't retain the last KeyboardEvent in ModifierKeyEmitter - #337852

Merged
Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/leak-modifier-key-emitter-event
Sep 25, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/leak-modifier-key-emitter-event

Conversation

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Related to #146841

ModifierKeyEmitter stored the last modifier KeyboardEvent in its (singleton) key status. A KeyboardEvent references its target and event path, so the DOM that received the last Ctrl/Alt/Shift/Meta key stayed alive until the next modifier key press — e.g. the complete view DOM of an editor whose model changed or which was closed, or a killed terminal.

The event is now only exposed while listeners are notified. Its only consumer (menubar Alt handling) reads it synchronously in the listener.

Retainer path (heap snapshot):
ModifierKeyEmitter.instance → _keyStatus.event → KeyboardEvent → EventPath → <div class="native-edit-context"> → … → <div class="monaco-editor">

Measurement

Scenario Without this change With this change
Focus an editor, press and release Ctrl, click another editor tab 1 detached editor view, 1,058 DOM nodes 0
Show rename + suggest widgets (Ctrl+Space), close all editors of the group 1 detached editor view, 1,385 nodes 0
Close all editors of the group with Ctrl+K W 1 detached editor view, 1,426 nodes 0

Bounded to one retained tree at a time (replaced on the next modifier key press), but it's the most common way an old editor view stays alive — any keyboard-driven tab switch or close.

How this was measured

Code OSS (Electron, dev build) driven over CDP with Playwright. After each scenario: 4× forced GC, then DOM.getDetachedDomNodes (the data behind Edge's Detached Elements tool), counting the descendants of every detached root. "Without this change" means all other fixes from this series applied and only this one reverted, so the number shows what this retainer alone keeps alive. Each scenario was run 3 times with identical results. The retainer was identified from a heap snapshot retainer path.

Added a unit test that the event is only available while listeners run.

The emitter kept the last modifier KeyboardEvent in its key status. The event references its target and event path, which kept the DOM of e.g. a closed editor or a killed terminal alive until the next modifier key press. Only expose the event while listeners are notified.

Related to #146841

Co-authored-by: Copilot <[email protected]>
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added the freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues label Sep 25, 2026
Copilot AI balanced review requested due to automatic review settings September 25, 2026 00:27
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added the freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues label Sep 25, 2026

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

🟢 Approval recommended

The leak fix is focused, safely cleared with finally, and covered by a targeted unit test.

Review effort: Balanced
Findings: None

What changed in this PR

Prevents ModifierKeyEmitter from retaining detached DOM through the last keyboard event.

Changes:

  • Clears keyboard events after synchronous listener notification.
  • Adds regression coverage for event lifetime.
File Description
src/​vs/​base/​browser/​dom.ts Limits keyboard-event retention to listener dispatch.
src/​vs/​base/​test/​browser/​dom.test.ts Tests transient event availability.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 2a4734b into main Sep 25, 2026
56 of 57 checks passed
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) deleted the agents/leak-modifier-key-emitter-event branch September 25, 2026 01:45
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Sep 27, 2026
…337852)

The emitter kept the last modifier KeyboardEvent in its key status. The event references its target and event path, which kept the DOM of e.g. a closed editor or a killed terminal alive until the next modifier key press. Only expose the event while listeners are notified.

Related to microsoft#146841

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 2a4734b)
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Nov 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants