Skip to content

Commit fb538bc

Browse files
marcocondracheCaio Raphael
authored andcommitted
gpui: Fix thermal state notifications on intel macOS (zed-industries#49086)
Fixes zed-industries#45638 (comment) Closes zed-industries#49005 The issue was similar to the one we already had with the quit method https://github.com/zed-industries/zed/blob/8249ef56187b966c33f6667d0d3a35d88d8f2dc0/crates/gpui/src/platform/mac/platform.rs#L491 @notpeter Could you please test this branch to confirm it resolves the issue? - [ ] Tests or screenshots needed? - [x] Code Reviewed - [ ] Manual QA Release Notes: - Fixed an issue where Zed would randomly crash on macOS intel
1 parent d26ea2d commit fb538bc

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

‎crates/gpui/src/platform/mac/platform.rs‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,16 +1258,33 @@ extern "C" fn on_keyboard_layout_change(this: &mut Object, _: Sel, _: id) {
12581258
}
12591259

12601260
extern "C" fn on_thermal_state_change(this: &mut Object, _: Sel, _: id) {
1261+
// Defer to the next run loop iteration to avoid re-entrant borrows of the App RefCell,
1262+
// as NSNotificationCenter delivers this notification synchronously and it may fire while
1263+
// the App is already borrowed (same pattern as quit() above).
1264+
use super::dispatcher::{dispatch_get_main_queue, dispatch_sys::dispatch_async_f};
1265+
12611266
let platform = unsafe { get_mac_platform(this) };
1262-
let mut lock = platform.0.lock();
1263-
if let Some(mut callback) = lock.on_thermal_state_change.take() {
1264-
drop(lock);
1265-
callback();
1266-
platform
1267-
.0
1268-
.lock()
1269-
.on_thermal_state_change
1270-
.get_or_insert(callback);
1267+
let platform_ptr = platform as *const MacPlatform as *mut c_void;
1268+
unsafe {
1269+
dispatch_async_f(
1270+
dispatch_get_main_queue(),
1271+
platform_ptr,
1272+
Some(on_thermal_state_change),
1273+
);
1274+
}
1275+
1276+
unsafe extern "C" fn on_thermal_state_change(context: *mut c_void) {
1277+
let platform = unsafe { &*(context as *const MacPlatform) };
1278+
let mut lock = platform.0.lock();
1279+
if let Some(mut callback) = lock.on_thermal_state_change.take() {
1280+
drop(lock);
1281+
callback();
1282+
platform
1283+
.0
1284+
.lock()
1285+
.on_thermal_state_change
1286+
.get_or_insert(callback);
1287+
}
12711288
}
12721289
}
12731290

0 commit comments

Comments
 (0)