From 0c489e0033afc9707d4931f99b945bf6f789c857 Mon Sep 17 00:00:00 2001 From: pajlada Date: Mon, 3 Feb 2025 00:19:04 +0100 Subject: [PATCH] fix: force quit miniaudio thread after 1s (#5896) --- CHANGELOG.md | 1 + src/controllers/sound/MiniaudioBackend.cpp | 21 ++++++++++++++++----- src/controllers/sound/MiniaudioBackend.hpp | 2 ++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e5f934c..bad35e7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Bugfix: Fixed announcements not showing up in mentions tab. (#5857) - Bugfix: Fixed the reply button showing for inline whispers and announcements. (#5863) - Bugfix: Fixed suspicious user treatment update messages not being searchable. (#5865) +- Bugfix: Ensure miniaudio backend exits even if it doesn't exit cleanly. (#5896) - Dev: Add initial experimental EventSub support. (#5837, #5895) - Dev: Highlight checks now use non-capturing groups for the boundaries. (#5784) - Dev: Updated Conan dependencies. (#5776) diff --git a/src/controllers/sound/MiniaudioBackend.cpp b/src/controllers/sound/MiniaudioBackend.cpp index 1c5c611c..69f23068 100644 --- a/src/controllers/sound/MiniaudioBackend.cpp +++ b/src/controllers/sound/MiniaudioBackend.cpp @@ -14,6 +14,7 @@ #define MINIAUDIO_IMPLEMENTATION #include #include +#include #include #include @@ -192,6 +193,10 @@ MiniaudioBackend::MiniaudioBackend() }); this->audioThread = std::make_unique([this] { + auto guard = qScopeGuard([&] { + this->stoppedFlag.set(); + }); + this->ioContext.run(); }); renameThread(*this->audioThread, "C2Miniaudio"); @@ -218,14 +223,20 @@ MiniaudioBackend::~MiniaudioBackend() this->workGuard.reset(); }); - if (this->audioThread->joinable()) - { - this->audioThread->join(); - } - else + if (!this->audioThread->joinable()) { qCWarning(chatterinoSound) << "Audio thread not joinable"; + return; } + + if (this->stoppedFlag.waitFor(std::chrono::seconds{1})) + { + this->audioThread->join(); + return; + } + + qCWarning(chatterinoSound) << "Audio thread did not stop within 1 second"; + this->audioThread->detach(); } void MiniaudioBackend::play(const QUrl &sound) diff --git a/src/controllers/sound/MiniaudioBackend.hpp b/src/controllers/sound/MiniaudioBackend.hpp index b8f359c3..53f0dd81 100644 --- a/src/controllers/sound/MiniaudioBackend.hpp +++ b/src/controllers/sound/MiniaudioBackend.hpp @@ -1,6 +1,7 @@ #pragma once #include "controllers/sound/ISoundController.hpp" +#include "util/OnceFlag.hpp" #include "util/ThreadGuard.hpp" #include @@ -59,6 +60,7 @@ private: boost::asio::executor_work_guard workGuard; std::unique_ptr audioThread; + OnceFlag stoppedFlag; boost::asio::steady_timer sleepTimer; bool initialized{false};