fix: prevent IPC socket cleanup panic

Prevent a panic when the IPC socket isn't available when the user quits
`ncspot`.
This commit is contained in:
Thomas Frans
2023-10-12 23:49:47 +02:00
committed by Henrik Friedrichsen
parent a69e2d763b
commit 51d1b34ccc

View File

@@ -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");
}
}
}