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.
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