From 78d6486822e35eb4a1deb757ef9906200cb1de24 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:04:10 -0400 Subject: [PATCH] fix crash trying to send message to offline user --- server/src/user_handler.rs | 40 ++++++++++++++------------------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 00bc2f7..36672fc 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -59,30 +59,20 @@ impl UserHandler { // Determine lookup method match method { Id(id) => { - tx = self - .state - .users_by_id - .read() - .unwrap() - .get(&id) - .unwrap() - .read() - .unwrap() - .tx - .clone(); + if let Some(user) = self.state.users_by_id.read().unwrap().get(&id) { + tx = user.read().unwrap().tx.clone(); + } else { + tracing::error!("Attempted to send message to invalid user id!"); + return; + } } Addr(addr) => { - tx = self - .state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .tx - .clone(); + if let Some(user) = self.state.online_users.read().unwrap().get(&addr) { + tx = user.read().unwrap().tx.clone(); + } else { + tracing::error!("Attempted to send message to offline user!"); + return; + } } } @@ -90,15 +80,15 @@ impl UserHandler { match message { SendUserUpdate(message) => { let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() + let _ = tx.send(msg).await; } SendChatMessage(message) => { let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() + let _ = tx.send(msg).await; } SendJudgeRound(message) => { let msg = to_string::(&message).unwrap(); - tx.send(msg).await.unwrap() // TODO: Fix explosion here when user disconnects + let _ = tx.send(msg).await; } } }