Skip to content

Commit

Permalink
fix deadlock in the server discovery fct
Browse files Browse the repository at this point in the history
recv_from blocks until there is something to read. So if slimserver is
started after the message has been sent there is no way any reply can
arrive.
  • Loading branch information
jecaro committed Jul 16, 2023
1 parent 72135f2 commit 9608dab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mprisqueeze"
version = "0.1.2"
version = "0.1.3"
authors = ["Jean-Charles Quillet <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
14 changes: 12 additions & 2 deletions src/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use nom::{
sequence::{preceded, tuple},
IResult,
};
use std::io::ErrorKind;
use tokio::net::UdpSocket;

#[derive(Debug)]
Expand All @@ -33,9 +34,18 @@ pub async fn discover() -> Result<Reply> {
sock.set_broadcast(true)?;

let mut buf = [0; 1024];
let _ = sock.send_to(&message, "255.255.255.255:3483").await?;
loop {
let _ = sock.send_to(&message, "255.255.255.255:3483").await?;

let _ = sock.recv_from(&mut buf).await?;
match sock.try_recv(&mut buf) {
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
continue;
}
anything_else => {
break anything_else;
}
}
}?;

parse_reply(&buf)
.map(|(_, reply)| {
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::{
pin,
process::{Child, Command},
select,
time::sleep,
time::{sleep, timeout},
};
mod discover;
mod lms;
Expand All @@ -25,12 +25,19 @@ struct Options {
#[arg(short, long, default_value = "SqueezeLite", help = "Player name")]
player_name: String,
#[arg(
short,
short = 't',
long,
default_value_t = 3,
help = "Timeout in seconds for squeezelite to be recognized by LMS"
)]
timeout: u64,
player_timeout: u64,
#[arg(
short = 'T',
long,
default_value_t = 3,
help = "Timeout in seconds for LMS discovery"
)]
discover_timeout: u64,
#[arg(
last = true,
default_values_t = vec!["squeezelite".to_string(), "-n".to_string(), "{}".to_string()],
Expand Down Expand Up @@ -105,7 +112,8 @@ async fn main() -> Result<()> {
..
} => (hostname.clone(), port),
_ => {
let reply = discover().await?;
let reply =
timeout(Duration::from_secs(options.discover_timeout), discover()).await??;
(reply.hostname, reply.port)
}
};
Expand All @@ -116,7 +124,7 @@ async fn main() -> Result<()> {
let result: Result<()> = (|| async {
// wait for the player to be available
let (client, mut recv) = LmsClient::new(hostname, port);
wait_for_player(&client, &options.player_name, options.timeout).await?;
wait_for_player(&client, &options.player_name, options.player_timeout).await?;

// start the MPRIS server
let _connection = start_dbus_server(client, options.player_name).await?;
Expand Down

0 comments on commit 9608dab

Please sign in to comment.