From e6599d8ed5f3391d7f060678c2b662263a4ecd2b Mon Sep 17 00:00:00 2001 From: Jonas Spanoghe Date: Thu, 19 Sep 2019 21:43:12 +0200 Subject: [PATCH] modules/mpd: take lock in waitForEvent to prevent SIGABORT + replaced deprecated MPD_IDLE_PLAYLIST with MPD_IDLE_QUEUE + add mutex for periodic_updater --- include/modules/mpd.hpp | 3 +++ src/modules/mpd.cpp | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp index 3cf71e0..d69618a 100644 --- a/include/modules/mpd.hpp +++ b/include/modules/mpd.hpp @@ -65,6 +65,9 @@ class MPD : public ALabel { unique_status status_; mpd_state state_; unique_song song_; + + // To make sure the previous periodic_updater stops before creating a new one + std::mutex periodic_lock_; }; } // namespace waybar::modules diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index dbf84c3..7bad855 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -41,6 +41,10 @@ auto waybar::modules::MPD::update() -> void { if (connection_ != nullptr) { try { bool wasPlaying = playing(); + if(!wasPlaying) { + // Wait until the periodic_updater has stopped + std::lock_guard periodic_guard(periodic_lock_); + } fetchState(); if (!wasPlaying && playing()) { periodic_updater().detach(); @@ -75,6 +79,7 @@ std::thread waybar::modules::MPD::event_listener() { std::thread waybar::modules::MPD::periodic_updater() { return std::thread([this] { + std::lock_guard guard(periodic_lock_); while (connection_ != nullptr && playing()) { dp.emit(); std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -297,7 +302,7 @@ void waybar::modules::MPD::waitForEvent() { // Wait for a player (play/pause), option (random, shuffle, etc.), or playlist // change if (!mpd_send_idle_mask( - conn, static_cast(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST))) { + conn, static_cast(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_QUEUE))) { checkErrors(conn); return; } @@ -306,6 +311,10 @@ void waybar::modules::MPD::waitForEvent() { // See issue #277: // https://github.com/Alexays/Waybar/issues/277 mpd_recv_idle(conn, /* disable_timeout = */ false); + // See issue #281: + // https://github.com/Alexays/Waybar/issues/281 + std::lock_guard guard(connection_lock_); + checkErrors(conn); mpd_response_finish(conn);