From a13f8e7937290a7737220f83a90b778ef3e46aac Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 25 Oct 2022 02:43:55 -0500 Subject: [PATCH] Ensure delayed flushes complete during shutdown --- app/backend/computermanager.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/backend/computermanager.cpp b/app/backend/computermanager.cpp index 440ee717..e2f902f5 100644 --- a/app/backend/computermanager.cpp +++ b/app/backend/computermanager.cpp @@ -188,6 +188,9 @@ ComputerManager::~ComputerManager() // Wait for it to terminate (and finish any pending flush) m_DelayedFlushThread->wait(); delete m_DelayedFlushThread; + + // Delayed flushes should have completed by now + Q_ASSERT(!m_NeedsDelayedFlush); } QWriteLocker lock(&m_Lock); @@ -220,7 +223,7 @@ ComputerManager::~ComputerManager() } void DelayedFlushThread::run() { - while (!QThread::currentThread()->isInterruptionRequested()) { + for (;;) { // Wait for a delayed flush request or an interruption { QMutexLocker locker(&m_ComputerManager->m_DelayedFlushMutex); @@ -229,15 +232,17 @@ void DelayedFlushThread::run() { m_ComputerManager->m_DelayedFlushCondition.wait(&m_ComputerManager->m_DelayedFlushMutex); } + // Bail without flushing if we woke up for an interruption alone. + // If we have both an interruption and a flush request, do the flush. + if (!m_ComputerManager->m_NeedsDelayedFlush) { + Q_ASSERT(QThread::currentThread()->isInterruptionRequested()); + break; + } + // Reset the delayed flush flag to ensure any racing saveHosts() call will set it again m_ComputerManager->m_NeedsDelayedFlush = false; } - if (QThread::currentThread()->isInterruptionRequested()) { - // Bail without flushing if we woke up for an interruption - break; - } - // Perform the flush { QReadLocker lock(&m_ComputerManager->m_Lock);