From c4af90f5febaa99b80e1ae9fbed8f638150728f5 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Tue, 3 May 2022 22:20:14 +0200 Subject: [PATCH] Avoid crashing when Avahi is not available When librespot is built with Avahi turned on, it will crash if Avahi is later not available at runtime. This change avoids it crashing hard when Avahi is not available; librespot will merely warn of the issue. This affects some distribution packages too, where the maintainer might prefer leaving Avahi support enabled, but many setups don't (or can't) run Avahi. Co-authored-by: Nick Steel --- CHANGELOG.md | 1 + discovery/src/lib.rs | 3 +-- src/main.rs | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d93b636c..a1d92e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [playback] Adhere to ReplayGain spec when calculating gain normalisation factor. - [playback] `alsa`: Use `--volume-range` overrides for softvol controls - [connect] Don't panic when activating shuffle without previous interaction. +- [main] Fix crash when built with Avahi support but Avahi is locally unavailable. ### Removed - [playback] `alsamixer`: previously deprecated option `mixer-card` has been removed. diff --git a/discovery/src/lib.rs b/discovery/src/lib.rs index b1249a0d..b2e65586 100644 --- a/discovery/src/lib.rs +++ b/discovery/src/lib.rs @@ -111,8 +111,7 @@ impl Builder { None, port, &["VERSION=1.0", "CPath=/"], - ) - .unwrap(); + ).map_err(|e| Error::DnsSdError(io::Error::new(io::ErrorKind::Unsupported, e)))?; } else { let responder = libmdns::Responder::spawn(&tokio::runtime::Handle::current())?; diff --git a/src/main.rs b/src/main.rs index 59ab0ce6..7ec21a5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1581,19 +1581,15 @@ async fn main() { if setup.enable_discovery { let device_id = setup.session_config.device_id.clone(); - - discovery = match librespot::discovery::Discovery::builder(device_id) + match librespot::discovery::Discovery::builder(device_id) .name(setup.connect_config.name.clone()) .device_type(setup.connect_config.device_type) .port(setup.zeroconf_port) .launch() { - Ok(d) => Some(d), - Err(e) => { - error!("Discovery Error: {}", e); - exit(1); - } - } + Ok(d) => discovery = Some(d), + Err(err) => warn!("Could not initialise discovery: {}.", err), + }; } if let Some(credentials) = setup.credentials { @@ -1606,6 +1602,11 @@ async fn main() { ) .fuse(), ); + } else if discovery.is_none() { + error!( + "Discovery is unavailable and no credentials provided. Authentication is not possible." + ); + exit(1); } loop {