From c1295c8fd6efebbaed60bd445323de75562c8118 Mon Sep 17 00:00:00 2001 From: Ian Hattendorf Date: Mon, 25 Feb 2019 20:05:44 -0700 Subject: [PATCH] Fix crash when monitor unplugged `Backlight#devices_` was being destructed before `Backlight#udev_thread_`. Also check if thread is still running after `epoll_wait` --- include/modules/backlight.hpp | 9 +++++---- src/modules/backlight.cpp | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index 4d0cba7d..80d2b1ec 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -56,11 +56,12 @@ private: const std::string preferred_device_; static constexpr int EPOLL_MAX_EVENTS = 16; - waybar::util::SleeperThread udev_thread_; - std::mutex udev_thread_mutex_; - std::vector devices_; - std::optional previous_best_; std::string previous_format_; + + std::mutex udev_thread_mutex_; + std::vector devices_; + // thread must destruct before shared data + waybar::util::SleeperThread udev_thread_; }; } // namespace waybar::modules diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index 9581a3d8..e4bcf7c3 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -149,6 +149,9 @@ waybar::modules::Backlight::Backlight(const std::string &name, const int event_count = epoll_wait(epoll_fd.get(), events, EPOLL_MAX_EVENTS, std::chrono::milliseconds{interval_}.count()); + if (!udev_thread_.isRunning()) { + break; + } decltype(devices_) devices; { std::scoped_lock lock(udev_thread_mutex_);