Skip to content

Commit 4e27f0b

Browse files
committed
fix(mpd): bound the connect timeout so an unreachable server can't freeze the bar
The MPD state machine drives all connection attempts from Glib::signal_timeout callbacks, which run on the GTK main loop. tryConnect() called mpd_connection_new() with the user-facing timeout_ (up to 30s by default), so an unreachable server blocked the whole bar for the full connect timeout. Bound the connect attempt to a short fixed timeout (2000 ms) so a dead server fails fast, then restore the configured timeout_ for subsequent command reads so slow-but-alive servers are unaffected. Fixes #1186.
1 parent 8541ec8 commit 4e27f0b

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/modules/mpd/mpd.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,31 @@ void waybar::modules::MPD::tryConnect() {
294294
return;
295295
}
296296
297-
connection_ =
298-
detail::unique_connection(mpd_connection_new(server_, port_, timeout_), &mpd_connection_free);
297+
// tryConnect() runs on the GTK main thread (via Glib::signal_timeout), so a
298+
// blocking connect freezes the whole bar. Bound the connect attempt to a
299+
// short timeout so an unreachable MPD server fails fast instead of hanging
300+
// the loop for the full user-facing `timeout_` (up to 30s by default, #1186).
301+
// The third argument to mpd_connection_new() is also the default command
302+
// read timeout, so restore `timeout_` once connected to avoid shortening
303+
// reads for slow-but-alive servers.
304+
static constexpr unsigned kConnectTimeoutMs = 2'000;
305+
unsigned connect_timeout =
306+
(timeout_ != 0 && timeout_ < kConnectTimeoutMs) ? timeout_ : kConnectTimeoutMs;
307+
308+
connection_ = detail::unique_connection(mpd_connection_new(server_, port_, connect_timeout),
309+
&mpd_connection_free);
299310
300311
if (connection_ == nullptr) {
301312
spdlog::error("{}: Failed to connect to MPD", module_name_);
302313
connection_.reset();
303314
return;
304315
}
305316
317+
// Restore the user-configured timeout for subsequent command reads.
318+
if (timeout_ != 0) {
319+
mpd_connection_set_timeout(connection_.get(), timeout_);
320+
}
321+
306322
try {
307323
checkErrors(connection_.get());
308324
spdlog::debug("{}: Connected to MPD", module_name_);

0 commit comments

Comments
 (0)