Skip to content

fix(terminal): send initial size when WebSocket opens#9505

Merged
mscolnick merged 3 commits into
mainfrom
ms/terminal-resize
May 11, 2026
Merged

fix(terminal): send initial size when WebSocket opens#9505
mscolnick merged 3 commits into
mainfrom
ms/terminal-resize

Conversation

@mscolnick

Copy link
Copy Markdown
Contributor

The mount-time fitAddon.fit() can fire before the terminal WS reaches
OPEN state (common in iframe + tunneled deployments), in which case the
resize message is silently dropped and the PTY stays at its default 0x0
winsize. bash then falls back to COLUMNS=80, producing horizontal-scroll
input lines and incorrect wrapping.

Send the current dimensions from the open handler so the PTY is always
sized before the user starts typing.

Copilot AI review requested due to automatic review settings May 11, 2026 20:03
@vercel

vercel Bot commented May 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 11, 2026 8:40pm

Request Review

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 1 file

Architecture diagram
sequenceDiagram
    participant User
    participant Frontend as Terminal React Component
    participant WS as WebSocket
    participant FIT as fitAddon
    participant Backend as Backend PTY Service

    Note over User,Backend: Initialisation Race Condition

    Frontend->>FIT: mount: fitAddon.fit()
    FIT-->>Frontend: cols, rows (maybe 0x0)
    Frontend->>WS: open() attempt
    alt WS not yet OPEN (iframe/tunnel)
        WS-->>Frontend: buffered / queued
        Frontend->>WS: resize message (dropped)
        WS-->>Backend: (nothing received)
        Note over Backend: PTY stays at 0x0 winsize
    else WS reaches OPEN state
        WS-->>Frontend: readyState = OPEN
        Frontend->>FIT: handleOpen: fitAddon.fit()
        FIT-->>Frontend: current dimensions (cols>0, rows>0)
        Frontend->>WS: send resize message
        WS->>Backend: JSON {type:"resize", cols, rows}
        Backend->>Backend: PTY winsize updated
        Backend-->>User: Correct terminal layout
    end

    Note over User,Backend: Without fix, bash defaults to COLUMNS=80
    Note over User,Backend: With fix, terminal correctly sized before user input
Loading

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

Ensures the backend PTY receives correct initial terminal dimensions even when the initial fitAddon.fit() happens before the terminal WebSocket reaches OPEN, preventing the PTY from staying at an incorrect default size.

Changes:

  • On WebSocket open, triggers a fitAddon.fit() and sends an explicit initial resize message with the current terminal cols/rows.
Comment thread frontend/src/components/terminal/terminal.tsx
Comment thread frontend/src/components/terminal/terminal.tsx
@mscolnick

mscolnick commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the redundant fitAddon.fit().

Skipping the test for now, very hard to mock this stuff out.

mscolnick added 3 commits May 11, 2026 16:38
The mount-time fitAddon.fit() can fire before the terminal WS reaches
OPEN state (common in iframe + tunneled deployments), in which case the
resize message is silently dropped and the PTY stays at its default 0x0
winsize. bash then falls back to COLUMNS=80, producing horizontal-scroll
input lines and incorrect wrapping.

Send the current dimensions from the open handler so the PTY is always
sized before the user starts typing.
fitAddon.fit() may trigger onResize and schedule a debounced send for
the same dimensions we just sent directly. Cancel the pending debounced
call to avoid a redundant resize ~100ms later.
window?.__MARIMO_STATIC__ throws ReferenceError when the window
identifier itself is undeclared (e.g. async work from RuntimeManager.init
firing after jsdom teardown in tests) — optional chaining only handles
null/undefined, not undeclared identifiers. Use a typeof guard so the
function safely returns undefined in non-browser contexts.

Fixes the Test FE unhandled-rejection failure on this PR.
@mscolnick mscolnick force-pushed the ms/terminal-resize branch from 7960cd0 to daef387 Compare May 11, 2026 20:39
@mscolnick mscolnick enabled auto-merge (squash) May 11, 2026 21:00
@mscolnick mscolnick disabled auto-merge May 11, 2026 21:05
@mscolnick mscolnick merged commit 6524402 into main May 11, 2026
31 checks passed
@mscolnick mscolnick deleted the ms/terminal-resize branch May 11, 2026 21:05
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.6-dev36

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

Labels

bug Something isn't working

2 participants