Skip to content

Conversation

@0xBigBoss
Copy link

Summary

Fixes a race condition that causes crashes when resizing the terminal while programs with high output (like cmatrix, htop) are running.

The issue occurs because the render loop and write operations can access WASM memory buffers while resize() is reallocating them, causing SIGSEGV crashes.

Changes

This PR implements three protections:

  1. Pause render loop during resize: Cancel the animation frame before WASM resize and restart after, preventing concurrent buffer access.

  2. Invalidate cached buffer views: Clear graphemeBuffer and graphemeBufferPtr when invalidating buffers, since TypedArray views become detached when underlying memory is reallocated.

  3. Queue writes during resize: Buffer incoming PTY data during resize and flush after a frame, preventing writes from hitting WASM while buffers are being reallocated.

All three protections are needed to fully prevent the race condition with rapidly-outputting programs.

Testing

  • All 328 tests pass
  • Manually tested resizing during cmatrix - no longer crashes
  • Tested with the BooTTY VS Code extension (fork of this project for VS Code terminal integration)

Files Changed

  • lib/terminal.ts - Main resize protection logic
  • lib/ghostty.ts - Buffer invalidation for graphemeBuffer

Fixes a race condition that causes crashes when resizing the terminal
while programs with high output (like cmatrix, htop) are running.

The issue occurs because the render loop and write operations can
access WASM memory buffers while resize() is reallocating them,
causing SIGSEGV crashes.

This fix implements three protections:

1. **Pause render loop during resize**: Cancel the animation frame
   before WASM resize and restart after, preventing concurrent
   buffer access.

2. **Invalidate cached buffer views**: Clear graphemeBuffer and
   graphemeBufferPtr when invalidating buffers, since TypedArray
   views become detached when underlying memory is reallocated.

3. **Queue writes during resize**: Buffer incoming PTY data during
   resize and flush after a frame, preventing writes from hitting
   WASM while buffers are being reallocated.

All three protections are needed to fully prevent the race condition
with rapidly-outputting programs.
@sreya
Copy link
Contributor

sreya commented Jan 12, 2026

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. You're on a roll.

ℹ️ 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".

Comment on lines +106 to +108
private _isResizing = false;
private _writeQueue: Array<{ data: string | Uint8Array; callback?: () => void }> = [];
private _resizeFlushFrameId?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any specific reason why we're prefixing these with _?

Copy link
Author

Choose a reason for hiding this comment

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

sry just used to prefixing private vars with _. i can remove it if u like

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah if you could, just for consistency

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.

2 participants