Skip to content

Release the input focus tracker once the input blurs - #337856

Merged
Dmitriy Vasyura (dmitrivMS) merged 3 commits into
mainfrom
agents/leak-context-key-input-focus-tracker
Sep 25, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 3 commits into
mainfrom
agents/leak-context-key-input-focus-tracker

Conversation

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Related to #146841

ContextKeyService.updateInputContextKeys creates a focus tracker for the focused input on every focusin. When the input blurred, the tracker was disposed, but the disposed tracker and the Event.once listener stayed in the per-focus DisposableStore until the next focusin. The tracker's _refreshStateHandler closure references the element, so the last focused input stayed alive — e.g. the native-edit-context of an editor view that was disposed while focused, when focus didn't move to another element.

The store is now cleared once the input blurs.

Retainer path (heap snapshot):
ContextKeyService … onFocusDisposables (MutableDisposable) → DisposableStore → FocusTracker._refreshStateHandler → element → <div class="native-edit-context">

Measurement

Scenario Without this change With this change
Focus an editor and close all editors of the group (Ctrl+K W) 1 detached node (native-edit-context) + the disposed FocusTracker 0

Small: the edit context element is removed from the view on dispose, so only the element itself is retained, not the view. Included for completeness of the #146841 cleanup.

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.

The disposed focus tracker and its listener stayed in the per-focus store until the next focusin event and kept the last focused input element alive, e.g. the edit context of a disposed editor view.

Related to #146841

Co-authored-by: Copilot <[email protected]>
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 lifecycle cleanup is correct and narrowly scoped; only a minor comment-style issue remains.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Prevents a focused input element from being retained after blur by immediately clearing its focus-scoped disposables.

Changes:

  • Clears the focus tracker and one-time blur listener on blur.
  • Releases references to disposed input DOM elements.
File Description
src/​vs/​platform/​contextkey/​browser/​contextKeyService.ts Clears input-focus disposables immediately after blur.

💡 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 ef46e22 into main Sep 25, 2026
35 checks passed
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) deleted the agents/leak-context-key-input-focus-tracker branch September 25, 2026 01:55
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Sep 27, 2026
* Release the input focus tracker once the input blurs

The disposed focus tracker and its listener stayed in the per-focus store until the next focusin event and kept the last focused input element alive, e.g. the edit context of a disposed editor view.

Related to microsoft#146841

Co-authored-by: Copilot <[email protected]>

* Remove narrating comment

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit ef46e22)
@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

context-keys 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