Skip to content

Release accessible view listeners per show instead of on dispose - #337857

Merged
Dmitriy Vasyura (dmitrivMS) merged 6 commits into
mainfrom
agents/leak-accessible-view-provider-listeners
Sep 25, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 6 commits into
mainfrom
agents/leak-accessible-view-provider-listeners

Conversation

@dmitrivMS

@dmitrivMS Dmitriy Vasyura (dmitrivMS) commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Related to #146841 (replaces the AccessibilityHelpWidget item of the issue, which became AccessibleView)

AccessibleView is a long-lived singleton, but show() registered per-provider listeners into its own _store on every call:

  • provider.onDidChangeContent listener
  • provider.onDidRequestClearLastProvider listener
  • a code block context provider registration (chat)

and _render() returned a new listener store on every content refresh, which was dropped when refreshing an already visible view. So every provider ever shown was retained for the lifetime of the window — for the terminal that is the whole TerminalInstance, xterm and DOM of every terminal whose accessible view was ever opened, even after the terminal was killed.

Now listeners of a show() are released when the view hides, render listeners replace the previous set, the clear-last-provider listener lives as long as that provider is remembered, and the code block context provider is registered once.

Retainer path (heap snapshot, before):
AccessibleViewService → AccessibleView._store → FunctionDisposable → listener closure → TerminalAccessibleBufferProvider._instance → TerminalInstance._wrapperElement → <div class="terminal-wrapper">

The clear-last-provider listener now uses the structural provider contract, so providers implementing IAccessibleViewContentProvider directly (like TerminalAccessibleBufferProvider) are honored. The terminal provider's clear notification is tied to the terminal instance instead of the provider, because the accessible view disposes the provider on hide while it may still remember it. Copies of the provider made for Accessibility Help (and restored when it closes) forward the clear request too.

Measurement

Scenario Without this change With this change
5× (create terminal, echo hello, Open Accessible View, Escape, kill terminal) 5 TerminalInstance, 5 XtermTerminal, 5 TerminalAccessibleBufferProvider, 5 BufferContentTracker alive; 5 detached terminal DOM trees (+26 detached roots / +96 nodes) 0 of each

This is unbounded: it grows by one terminal per terminal whose accessible view was opened (live object counts from heap snapshots after GC).

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 provider listeners don't accumulate across show() calls and are released when the view hides.

AccessibleView registered provider listeners (content changes, clear last provider, code block context) into its own store on every show and dropped the listeners of content refreshes. Every provider ever shown was retained for the lifetime of the window, e.g. every terminal whose accessible view was opened, including its TerminalInstance, xterm and DOM.

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

🟡 Changes recommended

The terminal provider bypasses the new clear listener and can still be retained after terminal disposal.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Refactors AccessibleView listener lifetimes to prevent retained providers and detached UI resources.

Changes:

  • Scopes provider and render listeners to each view lifecycle.
  • Reuses code-block registration and replaces transient disposable stores.
  • Adds listener cleanup coverage.
File Description
accessibleView.ts Refactors listener and disposable ownership.
accessibleView.test.ts Tests provider-listener cleanup.

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

The clear-last-provider listener was only installed for AccessibleContentProvider instances, which excludes TerminalAccessibleBufferProvider. The terminal provider also lost its clear notification when the accessible view disposed it on hide, so tie it to the terminal instance lifetime.

Co-authored-by: Copilot <[email protected]>
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added accessibility Keyboard, mouse, ARIA, vision, screen readers (non-specific) issues terminal General terminal issues that don't fall under another label labels Sep 25, 2026
@dmitrivMS Dmitriy Vasyura (dmitrivMS) added this to the 1.140.0 milestone Sep 25, 2026
…lp copies

- Create the context view cleanup once in render instead of returning a new tracked disposable from every _render call; refreshes discarded it.
- Copies of the current provider made for accessibility help use the structural provider check and forward onDidRequestClearLastProvider, so a terminal restored after the help view can still be cleared.
- Drop the unused _updateLastProvider() call in onHide that created an untracked provider copy on every hide.
- Tests render through a context view mock that behaves like the real one.

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

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

Accessibility Help can restore a terminal provider after its clear event already fired, retaining the disposed terminal.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

…s open

Closing accessibility help restored its copy of the last provider unconditionally. If the provider requested to be cleared while help was open (e.g. its terminal was killed), the copy was remembered again after the clear notification was gone, retaining the killed terminal.

Co-authored-by: Copilot <[email protected]>
Editor contributions register their own key listeners when the accessible view's text model gets attached, which happens asynchronously and, on macOS, added listeners after the baseline was taken. Take the baseline once the model is attached.

Co-authored-by: Copilot <[email protected]>
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) marked this pull request as ready for review September 25, 2026 04:25
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminalContrib/accessibility/browser/terminalAccessibleBufferProvider.ts

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 2bebb11 into main Sep 25, 2026
55 of 56 checks passed
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) deleted the agents/leak-accessible-view-provider-listeners branch September 25, 2026 04:32
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Sep 27, 2026
…rosoft#337857)

* Release accessible view listeners per show instead of on dispose

AccessibleView registered provider listeners (content changes, clear last provider, code block context) into its own store on every show and dropped the listeners of content refreshes. Every provider ever shown was retained for the lifetime of the window, e.g. every terminal whose accessible view was opened, including its TerminalInstance, xterm and DOM.

Related to microsoft#146841

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

* Honor clear requests of directly implemented providers

The clear-last-provider listener was only installed for AccessibleContentProvider instances, which excludes TerminalAccessibleBufferProvider. The terminal provider also lost its clear notification when the accessible view disposed it on hide, so tie it to the terminal instance lifetime.

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

* Address review: own render cleanup once and keep clear requests on help copies

- Create the context view cleanup once in render instead of returning a new tracked disposable from every _render call; refreshes discarded it.
- Copies of the current provider made for accessibility help use the structural provider check and forward onDidRequestClearLastProvider, so a terminal restored after the help view can still be cleared.
- Drop the unused _updateLastProvider() call in onHide that created an untracked provider copy on every hide.
- Tests render through a context view mock that behaves like the real one.

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

* Don't restore a provider that was cleared while accessibility help was open

Closing accessibility help restored its copy of the last provider unconditionally. If the provider requested to be cleared while help was open (e.g. its terminal was killed), the copy was remembered again after the clear notification was gone, retaining the killed terminal.

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

* Make render listener test independent of model attach timing

Editor contributions register their own key listeners when the accessible view's text model gets attached, which happens asynchronously and, on macOS, added listeners after the baseline was taken. Take the baseline once the model is attached.

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

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 2bebb11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accessibility Keyboard, mouse, ARIA, vision, screen readers (non-specific) issues freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues terminal General terminal issues that don't fall under another label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants