Skip to content

Comments

fix: prevent terminal crash on resize during high-output programs#132

Merged
sreya merged 2 commits intomainfrom
crash
Feb 24, 2026
Merged

fix: prevent terminal crash on resize during high-output programs#132
sreya merged 2 commits intomainfrom
crash

Conversation

@sreya
Copy link
Contributor

@sreya sreya commented Feb 24, 2026

Fixes terminal crashes (SIGSEGV) when resizing while high-output programs like cmatrix, htop, or cat /dev/urandom | base64 are running.

Problem

When the terminal is resized, WASM reallocates internal buffers (screen cells, viewport, grapheme data). Any TypedArray views pointing at the old ArrayBuffer become detached. If the render loop fires or a write() hits WASM mid-resize, the terminal crashes.

Solution

  • Pause the render loop before WASM resize, restart after — prevents render from reading detached buffers
  • Invalidate grapheme buffers in invalidateBuffers() alongside viewport buffers — the existing method missed these
  • Defensive write queue — buffers any writes during resize, flushed synchronously after resize completes

This avoids the background-tab regression identified in #114, where using an isResizing flag cleared via requestAnimationFrame would cause writes to queue indefinitely in background/hidden tabs (browsers throttle or pause rAF), leading to terminal hangs and unbounded memory growth.

Changes

  • lib/terminal.ts: Add cancelRenderLoop()/flushWriteQueue(), wrap resize() in try/catch with render loop pause/restart
  • lib/ghostty.ts: Free and null grapheme buffer pointers in invalidateBuffers()

Inspired by the approach in #114.

Co-authored-by: 0xBigBoss

Cancel the render loop before WASM resize to prevent accessing detached
TypedArray views when buffers are reallocated. Restart render loop and
flush any queued writes synchronously after resize completes.

This avoids the background-tab regression from PR #114's approach of
using an isResizing flag cleared via requestAnimationFrame (rAF is
throttled/paused in background tabs, causing writes to queue
indefinitely).

Changes:
- terminal.ts: Add cancelRenderLoop()/flushWriteQueue(), wrap resize()
  in try/catch with render loop pause/restart, add defensive writeQueue
- ghostty.ts: Invalidate grapheme buffers in invalidateBuffers()
  alongside viewport buffers to prevent stale TypedArray access

Co-authored-by: 0xBigBoss <95193764+0xBigBoss@users.noreply.github.com>
@sreya
Copy link
Contributor Author

sreya commented Feb 24, 2026

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a884a3d9aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Add early return if animationFrameId is already set, preventing
multiple concurrent RAF loops when resize() is called re-entrantly
from an onResize listener.
@sreya
Copy link
Contributor Author

sreya commented Feb 24, 2026

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. 👍

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sreya sreya merged commit fc99955 into main Feb 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant