Compare commits

...

2 Commits

Author SHA1 Message Date
Ferdinand Schober
0d960cbc30 macos: cleanup event tap thread on exit 2025-10-08 00:21:17 +02:00
Ferdinand Schober
f33d2d5d5a macos: fix a crash when InputCapture is dropped 2025-10-07 23:38:50 +02:00

View File

@@ -390,9 +390,9 @@ fn create_event_tap<'a>(
if let Some(pos) = pos { if let Some(pos) = pos {
res_events.iter().for_each(|e| { res_events.iter().for_each(|e| {
event_tx // error must be ignored, since the event channel
.blocking_send((pos, *e)) // may already be closed when the InputCapture instance is dropped.
.expect("Failed to send event"); let _ = event_tx.blocking_send((pos, *e));
}); });
// Returning None should stop the event from being processed // Returning None should stop the event from being processed
// but core fundation still returns the event // but core fundation still returns the event
@@ -426,8 +426,8 @@ fn event_tap_thread(
client_state: Arc<Mutex<InputCaptureState>>, client_state: Arc<Mutex<InputCaptureState>>,
event_tx: Sender<(Position, CaptureEvent)>, event_tx: Sender<(Position, CaptureEvent)>,
notify_tx: Sender<ProducerEvent>, notify_tx: Sender<ProducerEvent>,
ready: std::sync::mpsc::Sender<Result<(), MacosCaptureCreationError>>, ready: std::sync::mpsc::Sender<Result<CFRunLoop, MacosCaptureCreationError>>,
exit: oneshot::Sender<Result<(), &'static str>>, exit: oneshot::Sender<()>,
) { ) {
let _tap = match create_event_tap(client_state, notify_tx, event_tx) { let _tap = match create_event_tap(client_state, notify_tx, event_tx) {
Err(e) => { Err(e) => {
@@ -435,18 +435,22 @@ fn event_tap_thread(
return; return;
} }
Ok(tap) => { Ok(tap) => {
ready.send(Ok(())).expect("channel closed"); let run_loop = CFRunLoop::get_current();
ready.send(Ok(run_loop)).expect("channel closed");
tap tap
} }
}; };
log::debug!("running CFRunLoop...");
CFRunLoop::run_current(); CFRunLoop::run_current();
log::debug!("event tap thread exiting!...");
let _ = exit.send(Err("tap thread exited")); let _ = exit.send(());
} }
pub struct MacOSInputCapture { pub struct MacOSInputCapture {
event_rx: Receiver<(Position, CaptureEvent)>, event_rx: Receiver<(Position, CaptureEvent)>,
notify_tx: Sender<ProducerEvent>, notify_tx: Sender<ProducerEvent>,
run_loop: CFRunLoop,
} }
impl MacOSInputCapture { impl MacOSInputCapture {
@@ -475,36 +479,44 @@ impl MacOSInputCapture {
}); });
// wait for event tap creation result // wait for event tap creation result
ready_rx.recv().expect("channel closed")?; let run_loop = ready_rx.recv().expect("channel closed")?;
let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move { let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move {
loop { loop {
tokio::select! { tokio::select! {
producer_event = notify_rx.recv() => { producer_event = notify_rx.recv() => {
let producer_event = producer_event.expect("channel closed"); let Some(producer_event) = producer_event else {
break;
};
let mut state = state.lock().await; let mut state = state.lock().await;
state.handle_producer_event(producer_event).await.unwrap_or_else(|e| { state.handle_producer_event(producer_event).await.unwrap_or_else(|e| {
log::error!("Failed to handle producer event: {e}"); log::error!("Failed to handle producer event: {e}");
}) })
} }
res = &mut tap_exit_rx => { _ = &mut tap_exit_rx => {
if let Err(e) = res.expect("channel closed") { break;
log::error!("Tap thread failed: {:?}", e);
break;
}
} }
} }
} }
// show cursor
let _ = CGDisplay::show_cursor(&CGDisplay::main());
}); });
Ok(Self { Ok(Self {
event_rx, event_rx,
notify_tx, notify_tx,
run_loop,
}) })
} }
} }
impl Drop for MacOSInputCapture {
fn drop(&mut self) {
self.run_loop.stop();
}
}
#[async_trait] #[async_trait]
impl Capture for MacOSInputCapture { impl Capture for MacOSInputCapture {
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {