From 16883248f03f14b54c8f19f7675a564fb3278208 Mon Sep 17 00:00:00 2001 From: Levi Neely Date: Fri, 7 Aug 2026 18:03:43 +0200 Subject: [PATCH] nativestreamer: fix thread cleanup race - Disconnect finished signal before waiting in stop() to prevent onThreadFinished from running after thread is deleted - Use deleteLater() in onThreadFinished for safe cleanup during restart - Remove redundant quit() call (QThread::create threads have no event loop) --- gui/nativestreamer.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/gui/nativestreamer.cpp b/gui/nativestreamer.cpp index 89d99a1..c04a42d 100644 --- a/gui/nativestreamer.cpp +++ b/gui/nativestreamer.cpp @@ -61,9 +61,13 @@ void NativeStreamer::stop() } if (m_thread) { - m_thread->quit(); - m_thread->wait(1000); - if (m_thread->isRunning()) { + // Disconnect to prevent onThreadFinished from running after we delete + disconnect(m_thread, &QThread::finished, this, &NativeStreamer::onThreadFinished); + + // Wait for thread to finish (it should exit after fid is closed) + if (!m_thread->wait(2000)) { + // Thread didn't exit in time, force terminate + qWarning() << "NativeStreamer: thread didn't exit cleanly, terminating"; m_thread->terminate(); m_thread->wait(); } @@ -84,8 +88,9 @@ void NativeStreamer::setGuard(std::function guard) void NativeStreamer::onThreadFinished() { + // Thread finished naturally (not via stop()) - clean up and maybe restart if (m_thread) { - delete m_thread; + m_thread->deleteLater(); m_thread = nullptr; }