Skip to content

gpui: Fix RefCell panic in thermal/keyboard state callbacks#49187

Merged
morgankrey merged 1 commit intomainfrom
fix/refcell-thermal-state-crash
Feb 14, 2026
Merged

gpui: Fix RefCell panic in thermal/keyboard state callbacks#49187
morgankrey merged 1 commit intomainfrom
fix/refcell-thermal-state-crash

Conversation

@morgankrey
Copy link
Contributor

The on_thermal_state_change and on_keyboard_layout_change callbacks in App::new_app() called borrow_mut() unconditionally. These callbacks are invoked asynchronously by macOS via dispatch queues when the system's thermal or keyboard state changes, which can happen while the app's RefCell is already borrowed during an update cycle, causing a panic.

This change uses try_borrow_mut() instead. If the borrow fails (because the app is already borrowed), the callback silently skips the update - the state can be queried on the next frame.

Fixes ZED-4WM
Closes #49181

Release Notes:

  • Fixed a crash on macOS caused by thermal or keyboard layout state changes occurring during UI updates.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Feb 14, 2026
@zed-community-bot zed-community-bot bot added the staff Pull requests authored by a current member of Zed staff label Feb 14, 2026
Use try_borrow_mut() instead of borrow_mut() in on_thermal_state_change
and on_keyboard_layout_change callbacks. These callbacks can be invoked
asynchronously by macOS while the app RefCell is already borrowed during
an update cycle, causing a panic.

Fixes Sentry ZED-4WM.
Closes #49181.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@morgankrey morgankrey force-pushed the fix/refcell-thermal-state-crash branch from b5bf865 to 3238876 Compare February 14, 2026 19:40
@morgankrey morgankrey merged commit 9eb6fc0 into main Feb 14, 2026
27 checks passed
@morgankrey morgankrey deleted the fix/refcell-thermal-state-crash branch February 14, 2026 19:55
morgankrey added a commit that referenced this pull request Feb 14, 2026
Follow-up to #49187. Instead of silently skipping updates when
try_borrow_mut fails, defer the update by spawning it on the
foreground executor. This ensures the state change is eventually
processed after the current borrow completes.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
morgankrey added a commit that referenced this pull request Feb 14, 2026
Follow-up to #49187. Always spawn the update on the foreground
executor instead of borrowing the app directly in the callback.
This avoids RefCell panics when macOS fires these callbacks
asynchronously while the app is already borrowed.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
morgankrey added a commit that referenced this pull request Feb 14, 2026
Follow-up to #49187.

Instead of silently skipping updates when `try_borrow_mut` fails, this
PR defers the update by spawning it on the foreground executor. This
ensures the state change is eventually processed after the current
borrow completes.

Release Notes:

- N/A
marcocondrache added a commit to marcocondrache/zed that referenced this pull request Feb 16, 2026
bennetbo pushed a commit that referenced this pull request Feb 16, 2026
…ed" (#49251)

Reverts #49189
Reverts #49187

#49189 introduces a panic
(#49189 (comment),
#49181 (comment))
#49187 It's wrong since it leads to missing updates

The original crash should be fixed with #49086

cc: @bennetbo 

Release Notes:

- N/A
rtfeldman pushed a commit that referenced this pull request Feb 17, 2026
The `on_thermal_state_change` and `on_keyboard_layout_change` callbacks
in `App::new_app()` called `borrow_mut()` unconditionally. These
callbacks are invoked asynchronously by macOS via dispatch queues when
the system's thermal or keyboard state changes, which can happen while
the app's RefCell is already borrowed during an update cycle, causing a
panic.

This change uses `try_borrow_mut()` instead. If the borrow fails
(because the app is already borrowed), the callback silently skips the
update - the state can be queried on the next frame.

Fixes [ZED-4WM](https://zed-dev.sentry.io/issues/ZED-4WM)
Closes #49181

Release Notes:

- Fixed a crash on macOS caused by thermal or keyboard layout state
changes occurring during UI updates.
rtfeldman pushed a commit that referenced this pull request Feb 17, 2026
Follow-up to #49187.

Instead of silently skipping updates when `try_borrow_mut` fails, this
PR defers the update by spawning it on the foreground executor. This
ensures the state change is eventually processed after the current
borrow completes.

Release Notes:

- N/A
rtfeldman pushed a commit that referenced this pull request Feb 17, 2026
…ed" (#49251)

Reverts #49189
Reverts #49187

#49189 introduces a panic
(#49189 (comment),
#49181 (comment))
#49187 It's wrong since it leads to missing updates

The original crash should be fixed with #49086

cc: @bennetbo 

Release Notes:

- N/A
Caio-Ze pushed a commit to Caio-Ze/postprod-ide that referenced this pull request Feb 21, 2026
…stries#49187)

The `on_thermal_state_change` and `on_keyboard_layout_change` callbacks
in `App::new_app()` called `borrow_mut()` unconditionally. These
callbacks are invoked asynchronously by macOS via dispatch queues when
the system's thermal or keyboard state changes, which can happen while
the app's RefCell is already borrowed during an update cycle, causing a
panic.

This change uses `try_borrow_mut()` instead. If the borrow fails
(because the app is already borrowed), the callback silently skips the
update - the state can be queried on the next frame.

Fixes [ZED-4WM](https://zed-dev.sentry.io/issues/ZED-4WM)
Closes zed-industries#49181

Release Notes:

- Fixed a crash on macOS caused by thermal or keyboard layout state
changes occurring during UI updates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zed is crashing consistently on Mac Intel

1 participant