Skip to content

Commit

Permalink
macos: don't try to propagate panics that occur while dropping TimerS…
Browse files Browse the repository at this point in the history
…tate or DisplayState
  • Loading branch information
micahrj committed Aug 21, 2024
1 parent b383b32 commit 227137b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions reflector-platform/src/backend/cocoa/display_links.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::panic::{self, AssertUnwindSafe};
use std::ptr;
use std::rc::Rc;

Expand Down Expand Up @@ -56,13 +57,14 @@ extern "C" fn retain(info: *const c_void) -> *const c_void {
}

extern "C" fn release(info: *const c_void) {
// Hold a reference to AppState, since DisplayState is being dropped
let state = unsafe { &*(info as *const DisplayState) };
let app_state = Rc::clone(&state.app_state);

app_state.catch_unwind(|| {
let result = panic::catch_unwind(AssertUnwindSafe(|| {
unsafe { Rc::decrement_strong_count(info as *const DisplayState) };
});
}));

// If a panic occurs while dropping the Rc<DisplayState>, the only thing left to do is abort.
if let Err(_panic) = result {
std::process::abort();
}
}

extern "C" fn perform(info: *const c_void) {
Expand Down
14 changes: 8 additions & 6 deletions reflector-platform/src/backend/cocoa/timer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ffi::c_void;
use std::panic::{self, AssertUnwindSafe};
use std::ptr;
use std::rc::Rc;
use std::time::Duration;
Expand All @@ -19,13 +20,14 @@ extern "C" fn retain(info: *const c_void) -> *const c_void {
}

extern "C" fn release(info: *const c_void) {
// Hold a reference to AppState, since TimerState is being dropped
let state = unsafe { &*(info as *mut TimerState) };
let app_state = Rc::clone(&state.app_state);

app_state.catch_unwind(|| {
let result = panic::catch_unwind(AssertUnwindSafe(|| {
unsafe { Rc::decrement_strong_count(info as *const TimerState) };
});
}));

// If a panic occurs while dropping the Rc<WindowState>, the only thing left to do is abort.
if let Err(_panic) = result {
std::process::abort();
}
}

extern "C" fn callback(_timer: CFRunLoopTimerRef, info: *mut c_void) {
Expand Down

0 comments on commit 227137b

Please sign in to comment.