Skip to content

fix: guard terminal scroll operations before renderer is attached (fixes #339313) - #339330

Merged
Bryan Chen (bryanchen-d) merged 1 commit into
microsoft:mainfrom
vscodebot-pr:fix/terminal-scrolltobottom-undefined-dimensions-aw-37028364306
Nov 4, 2026
Merged

Bryan Chen (bryanchen-d) merged 1 commit into
microsoft:mainfrom
vscodebot-pr:fix/terminal-scrolltobottom-undefined-dimensions-aw-37028364306

Conversation

@vscodebot-pr

Copy link
Copy Markdown
Contributor

Summary

TerminalInstance.sendText() (reached via runCommand) calls this.xterm?.scrollToBottom(), which forwards to XtermTerminal.scrollToBottom() → bundled xterm.js scrollToBottom → scrollLines → get dimensions. When this runs against a terminal whose renderer has not been initialized yet (the xterm instance exists but attachToElement/raw.open() has not run, so the render service and its dimensions object do not exist), xterm.js throws TypeError: Cannot read properties of undefined (reading 'dimensions'). 389 users / 448 hits across Windows, Mac, and Linux on 1.140.0.

Fixes #339313
Recommended reviewer: @meganrogge

Culprit 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.ts line, authored 2022) and the XtermTerminal.scrollToBottom wrapper (authored 2021) both long predate the baseline, and git blame on 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.

Field Value
Commit Not identified — pre-existing
Author n/a
PR n/a
Message n/a
Why The crash path exists since 2021–2022; no commit in the regression window changes the triggering logic. Treated as a pre-existing boundary race.

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')
Loading

Affected Files

File Role Evidence
node_modules/@xterm/xterm/lib/xterm.js crash site get dimensions (from stack) — bundled third-party, unmodifiable
src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts boundary / fix site L767-L768: scrollToBottom(): void { this.raw.scrollToBottom(); } delegates straight into xterm.js with no attach guard
src/vs/workbench/contrib/terminal/browser/terminalInstance.ts trigger L1422: this.xterm?.scrollToBottom(); inside sendText, which can run before the instance is attached to the DOM

Repro Steps

This is a timing-dependent race, so it is non-deterministic:

  1. Trigger sendText/runCommand on a TerminalInstance whose 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.
  2. sendText reaches this.xterm?.scrollToBottom().
  3. xterm.js walks scrollToBottom → scrollLines → get dimensions and dereferences the render service's dimensions, which is undefined because the renderer was never opened.
  4. 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 guard if (!this._attached) { return; } to the renderer-delegating scroll wrappers (scrollToBottom, scrollUpLine, scrollDownLine, scrollUpPage, scrollDownPage, scrollToTop). _attached is set only after attachToElement runs this.raw.open(container) — the exact point at which xterm.js constructs its render service and the dimensions object 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 a try/catch and does not touch any logService.error path.

Alternatives considered:

  • Wrapping the scrollToBottom call in terminalInstance.sendText in try/catch — rejected because it hides the error from telemetry instead of preventing the invalid lifecycle call, and would mask other legitimate failures.
  • Optional-chaining into xterm.js internals (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 the sendText → scrollToBottom call path and is an active core maintainer with write access (commits within the last 90 days).

@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminal/browser/xterm/xtermTerminal.ts

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

🟡 Changes recommended

The reported crash path needs a regression test covering scrolling before attachment.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Prevents terminal scroll crashes before the xterm renderer is attached.

Changes:

  • Guards six scroll operations using the _attached lifecycle 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;
}

@meganrogge Megan Rogge (meganrogge) left a comment

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.

Thanks for the fix!

@meganrogge
Megan Rogge (meganrogge) enabled auto-merge (squash) November 3, 2026 19:30
auto-merge was automatically disabled November 4, 2026 00:23

Pull request was closed

@bryanchen-d
Bryan Chen (bryanchen-d) merged commit cb6e719 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
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.

[Error] unhandlederror-Cannot read properties of undefined (reading 'dimensions')

6 participants