From 581998c8bcbc137cb4ec6331b6c12f3f3444720f Mon Sep 17 00:00:00 2001 From: Brendan Fletcher Date: Tue, 3 Sep 2024 02:48:10 -0400 Subject: [PATCH] Prevent deadlock when breakpoints are hit during main window exit --- gui/qt/debugger.cpp | 1 + gui/qt/emuthread.cpp | 8 +++++++- gui/qt/mainwindow.cpp | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/gui/qt/debugger.cpp b/gui/qt/debugger.cpp index f01d5f271..49b7b187f 100644 --- a/gui/qt/debugger.cpp +++ b/gui/qt/debugger.cpp @@ -367,6 +367,7 @@ void MainWindow::debugExecute(uint32_t offset, uint8_t cmd) { void MainWindow::debugCommand(int reason, uint32_t data) { if (!guiEmuValid) { + emu.resume(); return; } diff --git a/gui/qt/emuthread.cpp b/gui/qt/emuthread.cpp index 7b31e98ef..ef87e4555 100644 --- a/gui/qt/emuthread.cpp +++ b/gui/qt/emuthread.cpp @@ -11,6 +11,8 @@ #include "capture/animated-png.h" #include +#include +#include #include #include @@ -396,11 +398,15 @@ void EmuThread::load(emu_data_t fileType, const QString &filePath) { } void EmuThread::stop() { + // Need to run events to allow queued slots to be processed during exit + QEventLoop eventLoop; + connect(this, &QThread::finished, &eventLoop, [&]() { eventLoop.exit(0); }); if (!isRunning()) { return; } emu_exit(); - if (!wait(500)) { + QTimer::singleShot(500, &eventLoop, [&]() { eventLoop.exit(1); }); + if (eventLoop.exec()) { terminate(); wait(500); } diff --git a/gui/qt/mainwindow.cpp b/gui/qt/mainwindow.cpp index c17304655..663ed064f 100644 --- a/gui/qt/mainwindow.cpp +++ b/gui/qt/mainwindow.cpp @@ -1389,6 +1389,8 @@ void MainWindow::emuSaved(bool success) { } void MainWindow::closeEvent(QCloseEvent *e) { + guiEmuValid = false; + if (!m_shutdown) { m_shutdown = true; @@ -1422,7 +1424,6 @@ void MainWindow::closeEvent(QCloseEvent *e) { } } - guiEmuValid = false; emu.stop(); QMainWindow::closeEvent(e); }