From baafa2cb39784a13a81f381e643f5067395e2330 Mon Sep 17 00:00:00 2001 From: Kyle Machulis Date: Sun, 17 Mar 2024 14:50:05 -0700 Subject: [PATCH] chore: Change several panics to warn messages A lot of these can fire as the system spins down, and will crash --- .../lovense_connect_service_comm_manager.rs | 26 +++++++++++-------- .../lovense_dongle_state_machine.rs | 20 +++++++++----- .../lovense_hid_dongle_comm_manager.rs | 6 +++-- .../serialport/serialport_hardware.rs | 6 +++-- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/buttplug/src/server/device/hardware/communication/lovense_connect_service/lovense_connect_service_comm_manager.rs b/buttplug/src/server/device/hardware/communication/lovense_connect_service/lovense_connect_service_comm_manager.rs index d8832d644..df1610e37 100644 --- a/buttplug/src/server/device/hardware/communication/lovense_connect_service/lovense_connect_service_comm_manager.rs +++ b/buttplug/src/server/device/hardware/communication/lovense_connect_service/lovense_connect_service_comm_manager.rs @@ -9,10 +9,8 @@ use super::lovense_connect_service_hardware::LovenseServiceHardwareConnector; use crate::{ core::errors::ButtplugDeviceError, server::device::hardware::communication::{ - HardwareCommunicationManager, - HardwareCommunicationManagerBuilder, - HardwareCommunicationManagerEvent, - TimedRetryCommunicationManager, + HardwareCommunicationManager, HardwareCommunicationManagerBuilder, + HardwareCommunicationManagerEvent, TimedRetryCommunicationManager, TimedRetryCommunicationManagerImpl, }, }; @@ -140,13 +138,19 @@ pub(super) async fn get_local_info(host: &str) -> Option match serde_json::from_str(&text) { + Ok(info) => Some(info), + Err(e) => { + warn!("Should always get json back from service, if we got a response: ${e}"); + None + } + }, + Err(e) => { + warn!("If we got a 200 back, we should at least have text: ${e}"); + None + } + } } Err(err) => { error!( diff --git a/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_dongle_state_machine.rs b/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_dongle_state_machine.rs index 17b02388f..1bb4f784c 100644 --- a/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_dongle_state_machine.rs +++ b/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_dongle_state_machine.rs @@ -139,11 +139,13 @@ impl ChannelHub { } pub async fn send_output(&self, msg: OutgoingLovenseData) { - self + if self .dongle_outgoing .send(msg) .await - .expect("Won't get here without owner being alive."); + .is_err() { + warn!("Dongle message sent without owner being alive, assuming shutdown."); + } } pub async fn send_event(&self, msg: HardwareCommunicationManagerEvent) { @@ -254,11 +256,13 @@ impl LovenseDongleState for LovenseDongleWaitForDongle { self.is_scanning.store(false, Ordering::SeqCst); should_scan = false; // If we were requested to scan and then asked to stop, act like we at least tried. - self + if self .event_sender .send(HardwareCommunicationManagerEvent::ScanningFinished) .await - .expect("Won't get here without owner being alive."); + .is_err() { + warn!("Dongle message sent without owner being alive, assuming shutdown."); + } } } } @@ -660,10 +664,14 @@ impl LovenseDongleState for LovenseDongleDeviceLoop { } } } - _ => device_read_sender + _ => { + if device_read_sender .send(dongle_msg) .await - .expect("Won't get here if the parent isn't alive."), + .is_err() { + warn!("Dongle message sent without owner being alive, assuming shutdown."); + } + } } } IncomingMessage::CommMgr(comm_msg) => match comm_msg { diff --git a/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_hid_dongle_comm_manager.rs b/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_hid_dongle_comm_manager.rs index dc58cdcb6..0bc27d5dc 100644 --- a/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_hid_dongle_comm_manager.rs +++ b/buttplug/src/server/device/hardware/communication/lovense_dongle/lovense_hid_dongle_comm_manager.rs @@ -260,13 +260,15 @@ impl LovenseHIDDongleCommunicationManager { *(held_read_thread.lock().await) = Some(read_thread); *(held_write_thread.lock().await) = Some(write_thread); - machine_sender_clone + if machine_sender_clone .send(LovenseDeviceCommand::DongleFound( writer_sender, reader_receiver, )) .await - .expect("We've already spun up the state machine so we know this receiver exists."); + .is_err() { + warn!("We've already spun up the state machine, this receiver should exist, but if we're shutting down this will throw."); + } info!("Found Lovense HID Dongle"); Ok(()) } diff --git a/buttplug/src/server/device/hardware/communication/serialport/serialport_hardware.rs b/buttplug/src/server/device/hardware/communication/serialport/serialport_hardware.rs index afd4e7d9a..da217fa9f 100644 --- a/buttplug/src/server/device/hardware/communication/serialport/serialport_hardware.rs +++ b/buttplug/src/server/device/hardware/communication/serialport/serialport_hardware.rs @@ -312,10 +312,12 @@ impl HardwareInternal for SerialPortHardware { let data = msg.data.clone(); // TODO Should check endpoint validity async move { - sender + if sender .send(data) .await - .expect("Tasks should exist if we get here."); + .is_err() { + warn!("Tasks should exist if we get here, but may not if we're shutting down"); + } Ok(()) } .boxed()