Skip to content

Commit 52db1e8

Browse files
authored
Merge pull request #467 from jspngh/bugfix/mpd_module
Add more locking in mpd module
2 parents 334bc1e + e6599d8 commit 52db1e8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

include/modules/mpd.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class MPD : public ALabel {
6565
unique_status status_;
6666
mpd_state state_;
6767
unique_song song_;
68+
69+
// To make sure the previous periodic_updater stops before creating a new one
70+
std::mutex periodic_lock_;
6871
};
6972

7073
} // namespace waybar::modules

src/modules/mpd.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ auto waybar::modules::MPD::update() -> void {
4141
if (connection_ != nullptr) {
4242
try {
4343
bool wasPlaying = playing();
44+
if(!wasPlaying) {
45+
// Wait until the periodic_updater has stopped
46+
std::lock_guard periodic_guard(periodic_lock_);
47+
}
4448
fetchState();
4549
if (!wasPlaying && playing()) {
4650
periodic_updater().detach();
@@ -75,6 +79,7 @@ std::thread waybar::modules::MPD::event_listener() {
7579
7680
std::thread waybar::modules::MPD::periodic_updater() {
7781
return std::thread([this] {
82+
std::lock_guard guard(periodic_lock_);
7883
while (connection_ != nullptr && playing()) {
7984
dp.emit();
8085
std::this_thread::sleep_for(std::chrono::seconds(1));
@@ -297,7 +302,7 @@ void waybar::modules::MPD::waitForEvent() {
297302
// Wait for a player (play/pause), option (random, shuffle, etc.), or playlist
298303
// change
299304
if (!mpd_send_idle_mask(
300-
conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST))) {
305+
conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_QUEUE))) {
301306
checkErrors(conn);
302307
return;
303308
}
@@ -306,6 +311,10 @@ void waybar::modules::MPD::waitForEvent() {
306311
// See issue #277:
307312
// https://github.com/Alexays/Waybar/issues/277
308313
mpd_recv_idle(conn, /* disable_timeout = */ false);
314+
// See issue #281:
315+
// https://github.com/Alexays/Waybar/issues/281
316+
std::lock_guard guard(connection_lock_);
317+
309318
checkErrors(conn);
310319
mpd_response_finish(conn);
311320

0 commit comments

Comments
 (0)