Skip to content

fix: memory leak in semantic tokens - #336024

Merged
Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-extHostSemanticTokens-cancellation
Sep 28, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-extHostSemanticTokens-cancellation

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

When a semantic-token request is canceled before an extension finishes, the adapter still saves the late token data in its result cache. The canceled request cannot release that entry.

Change

Check cancellation after the provider finishes, before converting or caching its token data.

Before

When requesting and canceling semantic tokens 37 times, the adapter keeps 37 old buffers totaling 740,000 bytes. Total ArrayBuffer memory grows by 742,245 bytes:

semantic-before-bytes semantic-before-stores

After

No more canceled token buffers remain in the adapter cache. The same test avoids the 740,000-byte leak; unrelated growth of 2,245 bytes and two backing stores remains:

semantic-after-bytes semantic-after-stores

Test Video

semantic-desktop-video.webm

roblourens
roblourens previously approved these changes Sep 17, 2026

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 AI Review comment

@dmitrivMS Dmitriy Vasyura (dmitrivMS) added freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues semantic-tokens Semantic tokens issues labels Sep 24, 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 cancellation races are handled at both RPC boundaries and covered by focused tests.

Review effort: Balanced
Findings: None

What changed in this PR

Prevents canceled semantic-token requests from retaining late extension results in the adapter cache.

Changes:

  • Stops conversion and caching after extension-host cancellation.
  • Releases results canceled during RPC transit.
  • Adds coverage for full tokens, edits, cache reuse, and RPC races.
File Description
extHostLanguageFeatures.ts Skips caching canceled results.
mainThreadLanguageFeatures.ts Releases late canceled results.
extHostSemanticTokens.test.ts Tests cancellation and cache cleanup.

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

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 0d3c47e into microsoft:main Sep 28, 2026
35 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.141.0 milestone Sep 28, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-extHostSemanticTokens-cancellation branch September 29, 2026 11:15
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Oct 1, 2026
* Fix semantic token cache leak after cancellation

* Fix semantic token cleanup when cancellation is in transit

(cherry picked from commit 0d3c47e)
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Nov 12, 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 semantic-tokens Semantic tokens issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants