From 51d1b34cccab0dcdb6d3bf393d56e3123f1c5535 Mon Sep 17 00:00:00 2001 From: Thomas Frans Date: Thu, 12 Oct 2023 23:49:47 +0200 Subject: [PATCH] fix: prevent IPC socket cleanup panic Prevent a panic when the IPC socket isn't available when the user quits `ncspot`. --- src/ipc.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 31d70dfb..692ffce8 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -26,8 +26,7 @@ struct Status { impl Drop for IpcSocket { fn drop(&mut self) { - log::info!("Removing IPC socket: {:?}", self.path); - std::fs::remove_file(&self.path).expect("Could not remove IPC socket"); + self.try_remove_socket(); } } @@ -126,4 +125,14 @@ impl IpcSocket { } } } + + /// Try to remove the IPC socket if there is one for this instance of `ncspot`. Don't do + /// anything if the socket has already been removed for some reason. + fn try_remove_socket(&mut self) { + if std::fs::remove_file(&self.path).is_ok() { + info!("removed socket at {:?}", self.path); + } else { + info!("socket already removed"); + } + } }