Repository navigation
fix: guard terminal scroll operations before renderer is attached (fixes #339313) - #339330
Merged
Bryan Chen (bryanchen-d) merged 1 commit intoNov 4, 2026
Conversation
microsoft#339313) Co-authored-by: Copilot <[email protected]>
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: Anthony Kim (@anthonykim1)Matched files:
|
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The reported crash path needs a regression test covering scrolling before attachment.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Prevents terminal scroll crashes before the xterm renderer is attached.
Changes:
- Guards six scroll operations using the
_attachedlifecycle state. - Treats premature scrolling as a no-op.
| File | Description |
|---|---|
src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts |
Adds attachment guards to renderer-dependent scrolling methods. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+774
to
+776
| if (!this._attached) { | ||
| return; | ||
| } |
Megan Rogge (meganrogge)
approved these changes
Nov 3, 2026
Megan Rogge (meganrogge)
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix!
Megan Rogge (meganrogge)
enabled auto-merge (squash)
November 3, 2026 19:30
roblourens
approved these changes
Nov 3, 2026
Dmitriy Vasyura (dmitrivMS)
approved these changes
Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
TerminalInstance.sendText()(reached viarunCommand) callsthis.xterm?.scrollToBottom(), which forwards toXtermTerminal.scrollToBottom()→ bundled xterm.jsscrollToBottom→scrollLines→get dimensions. When this runs against a terminal whose renderer has not been initialized yet (the xterm instance exists butattachToElement/raw.open()has not run, so the render service and itsdimensionsobject do not exist), xterm.js throwsTypeError: Cannot read properties of undefined (reading 'dimensions'). 389 users / 448 hits across Windows, Mac, and Linux on 1.140.0.Fixes #339313
Recommended reviewer:
@meganroggeCulprit Commit
No single culprit commit — this is a pre-existing latent race that surfaced as a new stable bucket in 1.140.0. The entry path
sendText → scrollToBottom(terminalInstance.tsline, authored 2022) and theXtermTerminal.scrollToBottomwrapper (authored 2021) both long predate the baseline, andgit blameon the throwing call sites points to commits years older than the regression window. The anomaly is a timing/re-bucketing shift (bundled xterm.js scroll internals), not a logic change in a commit within the 1.139.1→1.140.0 range.Code Flow
sequenceDiagram participant Caller as runCommand / sendText participant Inst as TerminalInstance participant Xterm as XtermTerminal participant Raw as xterm.js (bundled) Caller->>Inst: sendText(text, shouldExecute) Inst->>Xterm: this.xterm?.scrollToBottom() Note over Xterm: ⚠️ Boundary: renderer not<br/>yet initialized (no attachToElement) Xterm->>Raw: this.raw.scrollToBottom() Raw->>Raw: scrollLines() -> get dimensions Note over Raw: 💥 TypeError: Cannot read<br/>properties of undefined (reading 'dimensions')Affected Files
node_modules/@xterm/xterm/lib/xterm.jsget dimensions(from stack) — bundled third-party, unmodifiablesrc/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.tsscrollToBottom(): void { this.raw.scrollToBottom(); }delegates straight into xterm.js with no attach guardsrc/vs/workbench/contrib/terminal/browser/terminalInstance.tsthis.xterm?.scrollToBottom();insidesendText, which can run before the instance is attached to the DOMRepro Steps
This is a timing-dependent race, so it is non-deterministic:
sendText/runCommandon aTerminalInstancewhose xterm object has been created but not yet attached to the DOM (attachToElement/raw.open()not yet called) — e.g. programmatic command execution against a backgrounded or just-created terminal, or a restart that re-creates the xterm before re-attaching.sendTextreachesthis.xterm?.scrollToBottom().scrollToBottom → scrollLines → get dimensionsand dereferences the render service'sdimensions, which isundefinedbecause the renderer was never opened.TypeError: Cannot read properties of undefined (reading 'dimensions')is thrown.Increase likelihood by executing commands against terminals immediately on creation or during a restart, before layout/attach completes.
How the Fix Works
Chosen approach (
src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts): Add an early-return guardif (!this._attached) { return; }to the renderer-delegating scroll wrappers (scrollToBottom,scrollUpLine,scrollDownLine,scrollUpPage,scrollDownPage,scrollToTop)._attachedis set only afterattachToElementrunsthis.raw.open(container)— the exact point at which xterm.js constructs its render service and thedimensionsobject the throwing code reads. The producer of the violating value is bundled third-party xterm.js, which cannot be modified in this repo, so the fix is placed at VS Code's own boundary wrapper and gated on the real lifecycle signal (_attached) rather than suppressing the symptom at the crash site. Scrolling a terminal that has no renderer yet is a no-op anyway, so the guard changes no user-visible behavior. The guard is not atry/catchand does not touch anylogService.errorpath.Alternatives considered:
scrollToBottomcall interminalInstance.sendTextintry/catch— rejected because it hides the error from telemetry instead of preventing the invalid lifecycle call, and would mask other legitimate failures.this.raw.scrollToBottom?.()) — rejected because the method is always callable; the undefined value is deep inside xterm.js's render service, unreachable from the wrapper, so chaining would not prevent the throw.Recommended Owner
@meganrogge— terminal area owner; authored thesendText → scrollToBottomcall path and is an active core maintainer with write access (commits within the last 90 days).