Skip to content

fix: memory leak in aiSearchModel - #342528

Merged
Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-aiSearchModel
Nov 5, 2026
Merged

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

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

Each search creates AI workspace folder roots and registers them in the search heading’s disposable store. Clearing the results disposes the roots, but does not remove them from that store, so old roots accumulate across searches.

Change

Use the existing search-result lifecycle to dispose AI folder roots and a one-shot disposal listener to remove the change listener.

Before

When running search/replace 37 times, AI folder-root allocations and their callbacks grow by 74:

before

After

No more leak detected.

Test Video

search-replace.webm

AI disclosure: Model: GPT 6.1 Sol. Worktime: 10 min

Copilot AI balanced review requested due to automatic review settings October 19, 2026 11:25

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.

🟢 Approval recommended

The focused fix preserves lifecycle cleanup and adds targeted regression coverage, with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes retained AI search folder roots by relying on the existing search-result disposal lifecycle.

Changes:

  • Removes heading-level retention of AI folder roots and uses a one-shot disposal listener.
  • Adds a regression test covering repeated search clearing and model disposal.
File Description
src/​vs/​workbench/​contrib/​search/​test/​browser/​searchModel.test.ts Verifies cleared AI roots are disposed only once.
src/​vs/​workbench/​contrib/​search/​browser/​AISearch/​aiSearchModel.ts Removes redundant root ownership and cleans up listeners on disposal.

Review effort: Balanced

@dmitrivMS Dmitriy Vasyura (dmitrivMS) added freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues chat labels Oct 23, 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.

🟡 Changes recommended

Consecutive query replacements overwrite pending cleanup, leaving the first AI root undisposed and potentially retained by open-editor model listeners even after model disposal.

1 open issue

Review effort: Max

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.

Please see review comment

@SimonSiefke

Copy link
Copy Markdown
Contributor Author

[ai] feedback implemented

Addressed the pending-root cleanup feedback in 7ccd778.

  • Preserve every pending root across consecutive query replacements and drain cleanup on result delivery, clearing, and disposal. Release disposed roots from ownership so they are not disposed again.
  • Check existing AI work before keyword suggestions reset the result model, preventing duplicate request attempts after the first streamed match.
  • Add six plain-text/AI cleanup regression cases and two suggestion-request regression tests.

Validation: 50 Chromium search unit tests passed; client build, full client typecheck, and targeted ESLint passed. The 16-case reproduction matrix passed, and 100 cleanup cycles in each of the natural and explicitly model-bound control cases left no retained roots/file matches or editor listeners while the result model remained alive. These targeted checks cover disposal and request handling; a full Electron UI run was not performed.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit f651fd4 into microsoft:main Nov 5, 2026
55 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 5, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-aiSearchModel branch November 5, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chat 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