Skip to content

fix: memory leak in notebook text diff editor - #331805

Merged
Dmitriy Vasyura (dmitrivMS) merged 11 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-notebookTextDiffEditor
Nov 4, 2026
Merged

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

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

Notebook diff view models were registered in the editor pane's lifetime disposable store, so closing a notebook diff editor left every previous view model and its notebook models reachable.

Change

Register each notebook diff view model in the input-specific store and run the existing model-detach cleanup when the editor input is cleared.

Before

When opening and closing a notebook diff editor 37 times, notebook diff view models and their notebook models grow once or twice per run:

before

After

No more notebook diff view model or notebook model leak is detected.

Test Video

test-video.webm

Copilot AI balanced review requested due to automatic review settings August 20, 2026 14:47

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.

Pull request overview

Fixes notebook diff editor model leaks by aligning disposables with each editor input’s lifetime.

Changes:

  • Disposes layout work during model detachment.
  • Registers diff view models in the input-specific disposable store.
  • Reuses model-detachment cleanup when clearing input.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the diff. Changes align with project standards.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

P3: reset the current-change index when clearing an input

The new clearInput() → _detachModel() call changes an observable-state side effect: the input-local store is now cleared before the list is cleared. That removes the focus subscription that resets currentChangedIndex to -1 before it receives the empty-focus event.

Previously, clearing the input spliced the list while that subscription was still alive. With this change, a cached pane can retain the previous input's current-change index. This observable is consumed by the chat notebook-diff integration, including navigation-boundary decisions, so it is not just an unused field.

Suggested repro / regression test (source-derived; not executed):

  1. Use the notebook text-diff editor (notebook.experimental.enableNewDiffEditor: false, notebook.diff.enablePreview: true).
  2. Open a notebook diff with at least three changed cells and focus the third change; verify its current-change index is 2.
  3. Close that diff while retaining the editor group/pane cache. In a debugger or a scoped pane test, inspect the same pane immediately after input clearing completes, before another list update.
  4. Expected: the index is -1. The new disposal ordering leaves it at 2. Replacing the input in the same group also carries that stale state until an applicable items/focus update resets it.

Could the detach path explicitly reset the observable, with a focused clear/reuse test? That would preserve the new input-specific ownership without depending on a listener that has already been disposed.

Validation limit: the ordering and only-writer relationship were checked statically; I have not reproduced a visible chat-navigation failure. The pane's own next/previous navigation uses list focus and is not claimed broken.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Race-coverage request: clearing the layout token before the final set-input continuation

This is a lower-confidence validation concern, not a reproduced user-visible regression.

The new assignment clearing the layout cancellation source needs to be considered alongside the await of modified-webview creation and the subsequent unguarded .token dereference.

Suggested deterministic interleaving for a scoped test (not executed):

  1. Begin setting a resolved notebook-diff input.
  2. Hold the continuation at the modified-webview await using a test-controlled promise, after the helper has created the webview.
  3. Clear/close that input, then release the continuation without attaching a replacement input.
  4. Assert that the obsolete open operation completes without dereferencing a cleared source. With the new assignment, the resumed expression can throw a TypeError before reaching the no-model guard inside updateLayout(). Before this change, that source remained present and this particular continuation could reach the guard and return.

An input/model-staleness check after the await, before accessing the source, would make the intended close-during-open behavior explicit.

Scope/limitations: the real helper currently has no internal await, so normal UI reachability of this microtask interleaving is unverified; the proposed test deliberately controls it. There is already a similar earlier-await failure involving a cleared model, so this is not a claim that the PR introduced the broader cancellation problem. Please treat this as a narrowly scoped test/guard request, not a confirmed general cancellation regression.

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 inline comments

@SimonSiefke

Copy link
Copy Markdown
Contributor Author

Created a separate PR to handle cancellation of diffEditorInputs better: #339389

@dmitrivMS

Copy link
Copy Markdown
Collaborator

P3: the change index still survives input clearing at the current head

Following up on the earlier index-reset finding: this remains present at current head 44a225758b78438ccca2a32e94e1871cc845cdd9, after the lifecycle changes were reverted.

The new clearInput() -> _detachModel() call changes the reset ordering:

  1. _localStore.clear() disposes the focus listener that resets _currentChangedIndex to -1.
  2. _list.clear() then clears the focus, but that listener is no longer present.

Previously, clearInput() spliced the list while the listener was still active. The new ordering leaves a cached pane's public currentChangedIndex at the previous input's value. The chat notebook-diff integration reads this observable, including for navigation-boundary decisions.

Suggested repro / regression test (source-derived; not executed):

  1. Use the notebook text-diff editor with notebook.experimental.enableNewDiffEditor: false and notebook.diff.enablePreview: true.
  2. Compare two notebooks with at least three changed cells. Focus the third change and verify the pane's currentChangedIndex.get() is 2.
  3. Close the diff without disposing its editor group/pane cache. In a debugger or focused pane test, inspect that same pane immediately after clearInput() returns, before a subsequent input/list update.
  4. Expected: currentChangedIndex.get() is -1. Source-predicted result at this head: it remains 2. Also cover replacing the input in the same pane so the old selection does not survive into the replacement's loading interval.

Please explicitly reset _currentChangedIndex to -1 during detachment and retain focused clear/reuse coverage. The reset briefly added in 30550d45 was removed by the head revert; moving it to separate #339389 does not include it in this PR's standalone delta.

Validation limit: both independent source reviews identified the ordering regression, but no Code OSS reproduction or visible chat-navigation failure was demonstrated. The pane's own next/previous commands use list focus and are not claimed broken. This is a low-severity observable-state regression, not a claim of a broad navigation outage.

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 consider new review comment

@SimonSiefke

Copy link
Copy Markdown
Contributor Author

Should be resolved with PR #339389, which is now merged.

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

Detaching does not cancel in-flight layout work, and the ownership fix lacks regression coverage.

2 open findings
1 resolved since last review

🧠 Review effort: Balanced

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

An uncancelled layout token allows a pending diff to create cell view models after their owner is disposed, leaking those models and their notebook listeners when the input is cleared.

3 open issues

Review effort: Max

@SimonSiefke
Simon Siefke (SimonSiefke) marked this pull request as draft November 3, 2026 10:33
@SimonSiefke

Copy link
Copy Markdown
Contributor Author

Addressed minor Copilot feedback!

@SimonSiefke
Simon Siefke (SimonSiefke) marked this pull request as ready for review November 3, 2026 12:41
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 4c32afe into microsoft:main Nov 4, 2026
59 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 4, 2026
@SimonSiefke
Simon Siefke (SimonSiefke) deleted the fix/memory-leak-notebookTextDiffEditor 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

freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues notebook-diff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants