fix(AsyncNotificationCenter): hang on stop() #5059

This commit is contained in:
Alex Fabijanic
2025-11-06 20:39:34 +01:00
committed by Matej Kenda
parent 7d08a85e5d
commit 1284103b98
2 changed files with 10 additions and 1 deletions

View File

@@ -120,6 +120,11 @@ private:
using Adapter = RunnableAdapter<AsyncNotificationCenter>; using Adapter = RunnableAdapter<AsyncNotificationCenter>;
class ShutdownNotification: public Notification
/// Internal notification used to signal the dequeue loop to stop.
{
};
const AsyncMode _mode { AsyncMode::ENQUEUE }; const AsyncMode _mode { AsyncMode::ENQUEUE };
// Async enqueue for notifications // Async enqueue for notifications

View File

@@ -180,6 +180,7 @@ void AsyncNotificationCenter::stop()
{ {
if (_enqueueThreadStarted.exchange(false)) if (_enqueueThreadStarted.exchange(false))
{ {
_nq.enqueueUrgentNotification(new ShutdownNotification);
_nq.wakeUpAll(); _nq.wakeUpAll();
while (!_enqueueThreadDone) Thread::sleep(100); while (!_enqueueThreadDone) Thread::sleep(100);
_enqueueThread.join(); _enqueueThread.join();
@@ -207,8 +208,11 @@ void AsyncNotificationCenter::dequeue()
Notification::Ptr pNf; Notification::Ptr pNf;
_enqueueThreadStarted = true; _enqueueThreadStarted = true;
_enqueueThreadDone = false; _enqueueThreadDone = false;
while ((pNf = _nq.waitDequeueNotification())) while (true)
{ {
pNf = _nq.waitDequeueNotification();
if (!pNf) break;
if (pNf.cast<ShutdownNotification>()) break;
try try
{ {
notifyObservers(pNf); notifyObservers(pNf);