Skip to content

fix: guard against race condition in notebook find revealCellRange (#225578)#320405

Open
Mr-Ahmadi wants to merge 1 commit into
microsoft:mainfrom
Mr-Ahmadi:fix/notebook-find-race-condition
Open

fix: guard against race condition in notebook find revealCellRange (#225578)#320405
Mr-Ahmadi wants to merge 1 commit into
microsoft:mainfrom
Mr-Ahmadi:fix/notebook-find-race-condition

Conversation

@Mr-Ahmadi
Copy link
Copy Markdown

This PR fixes #225578 — a TypeError where contentMatches is read on undefined in notebook findModel.ts.

Root Cause

Both callers of revealCellRange (find() and refreshCurrentMatch()) call it inside a .then() callback after highlightCurrentFindMatchDecoration(). Between the synchronous computation of cellIndex and execution of the microtask, this._findMatches can be replaced by research()set() (triggered by onDidChangeContent or other events), making the cached cellIndex out of bounds.

Fix

Added an early return guard in revealCellRange when findMatch is undefined, matching the defensive pattern used elsewhere in the codebase.

Validation

  • Change is scoped to a single file
  • Follows existing defensive coding patterns
  • No breaking changes to the public API

Copilot AI review requested due to automatic review settings June 8, 2026 12:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a defensive guard in the notebook find model to avoid dereferencing an undefined match entry while revealing a cell range.

Changes:

  • Add an early return in revealCellRange when _findMatches[cellIndex] is missing.

Comment on lines 274 to +278
private async revealCellRange(cellIndex: number, matchIndex: number, outputOffset: number | null) {
const findMatch = this._findMatches[cellIndex];
if (!findMatch) {
return;
}
…icrosoft#225578)

When find matches are being revealed and the underlying _findMatches
array is replaced between the cellIndex computation and the async
callback (e.g. via research() triggered by onDidChangeContent),
findMatch can be undefined. Add an early return guard to prevent
'Cannot read properties of undefined (reading contentMatches)'.

Fixes microsoft#225578
@Mr-Ahmadi Mr-Ahmadi force-pushed the fix/notebook-find-race-condition branch from db606b1 to 4b50b99 Compare June 8, 2026 13:33
@Mr-Ahmadi
Copy link
Copy Markdown
Author

Thanks for the review. Both callers (find() and refreshCurrentMatch()) already ignore revealCellRange's return value, so returning a boolean wouldn't help without restructuring the async flow. I've added a comment explaining the race condition for maintainability — pushed in the amended commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'contentMatches')

4 participants