Skip to content

Commit f3a17bb

Browse files
committed
qt: Do not exit and re-enter main event loop during shutdown
1 parent b4e0d2c commit f3a17bb

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

src/qt/bitcoin.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <QThread>
5555
#include <QTimer>
5656
#include <QTranslator>
57+
#include <QWindow>
5758

5859
#if defined(QT_STATICPLUGIN)
5960
#include <QtPlugin>
@@ -258,6 +259,7 @@ void BitcoinApplication::createOptionsModel(bool resetSettings)
258259
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
259260
{
260261
window = new BitcoinGUI(node(), platformStyle, networkStyle, nullptr);
262+
connect(window, &BitcoinGUI::quitRequested, this, &BitcoinApplication::requestShutdown);
261263

262264
pollShutdownTimer = new QTimer(window);
263265
connect(pollShutdownTimer, &QTimer::timeout, window, &BitcoinGUI::detectShutdown);
@@ -325,13 +327,17 @@ void BitcoinApplication::requestInitialize()
325327

326328
void BitcoinApplication::requestShutdown()
327329
{
330+
for (const auto w : QGuiApplication::topLevelWindows()) {
331+
w->hide();
332+
}
333+
328334
// Show a simple window indicating shutdown status
329335
// Do this first as some of the steps may take some time below,
330336
// for example the RPC console may still be executing a command.
331337
shutdownWindow.reset(ShutdownWindow::showShutdownWindow(window));
332338

333339
qDebug() << __func__ << ": Requesting shutdown";
334-
window->hide();
340+
335341
// Must disconnect node signals otherwise current thread can deadlock since
336342
// no event loop is running.
337343
window->unsubscribeFromCoreSignals();
@@ -408,13 +414,13 @@ void BitcoinApplication::initializeResult(bool success, interfaces::BlockAndHead
408414
pollShutdownTimer->start(200);
409415
} else {
410416
Q_EMIT splashFinished(); // Make sure splash screen doesn't stick around during shutdown
411-
quit(); // Exit first main loop invocation
417+
requestShutdown();
412418
}
413419
}
414420

415421
void BitcoinApplication::shutdownResult()
416422
{
417-
quit(); // Exit second main loop invocation after shutdown finished
423+
quit();
418424
}
419425

420426
void BitcoinApplication::handleRunawayException(const QString &message)
@@ -637,8 +643,6 @@ int GuiMain(int argc, char* argv[])
637643
#if defined(Q_OS_WIN)
638644
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely…").arg(PACKAGE_NAME), (HWND)app.getMainWinId());
639645
#endif
640-
app.exec();
641-
app.requestShutdown();
642646
app.exec();
643647
rv = app.getReturnValue();
644648
} else {

src/qt/bitcoin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ class BitcoinApplication: public QApplication
5656

5757
/// Request core initialization
5858
void requestInitialize();
59-
/// Request core shutdown
60-
void requestShutdown();
6159

6260
/// Get process return value
6361
int getReturnValue() const { return returnValue; }
@@ -73,6 +71,8 @@ class BitcoinApplication: public QApplication
7371

7472
public Q_SLOTS:
7573
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
74+
/// Request core shutdown
75+
void requestShutdown();
7676
void shutdownResult();
7777
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
7878
void handleRunawayException(const QString &message);

src/qt/bitcoingui.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ void BitcoinGUI::createActions()
371371
m_mask_values_action->setStatusTip(tr("Mask the values in the Overview tab"));
372372
m_mask_values_action->setCheckable(true);
373373

374-
connect(quitAction, &QAction::triggered, qApp, QApplication::quit);
374+
connect(quitAction, &QAction::triggered, this, &BitcoinGUI::quitRequested);
375375
connect(aboutAction, &QAction::triggered, this, &BitcoinGUI::aboutClicked);
376376
connect(aboutQtAction, &QAction::triggered, qApp, QApplication::aboutQt);
377377
connect(optionsAction, &QAction::triggered, this, &BitcoinGUI::optionsClicked);
@@ -991,6 +991,7 @@ void BitcoinGUI::openOptionsDialogWithTab(OptionsDialog::Tab tab)
991991
return;
992992

993993
auto dlg = new OptionsDialog(this, enableWallet);
994+
connect(dlg, &OptionsDialog::quitOnReset, this, &BitcoinGUI::quitRequested);
994995
dlg->setCurrentTab(tab);
995996
dlg->setModel(clientModel->getOptionsModel());
996997
GUIUtil::ShowModalDialogAndDeleteOnClose(dlg);
@@ -1216,7 +1217,7 @@ void BitcoinGUI::closeEvent(QCloseEvent *event)
12161217
// close rpcConsole in case it was open to make some space for the shutdown window
12171218
rpcConsole->close();
12181219

1219-
QApplication::quit();
1220+
Q_EMIT quitRequested();
12201221
}
12211222
else
12221223
{
@@ -1410,7 +1411,7 @@ void BitcoinGUI::detectShutdown()
14101411
{
14111412
if(rpcConsole)
14121413
rpcConsole->hide();
1413-
qApp->quit();
1414+
Q_EMIT quitRequested();
14141415
}
14151416
}
14161417

src/qt/bitcoingui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class BitcoinGUI : public QMainWindow
214214
void openOptionsDialogWithTab(OptionsDialog::Tab tab);
215215

216216
Q_SIGNALS:
217+
void quitRequested();
217218
/** Signal raised when a URI was entered or dragged to the GUI */
218219
void receivedURI(const QString &uri);
219220
/** Signal raised when RPC console shown */

src/qt/optionsdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ void OptionsDialog::on_resetButton_clicked()
290290

291291
/* reset all options and close GUI */
292292
model->Reset();
293-
QApplication::quit();
293+
close();
294+
Q_EMIT quitOnReset();
294295
}
295296
}
296297

src/qt/optionsdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private Q_SLOTS:
6868

6969
Q_SIGNALS:
7070
void proxyIpChecks(QValidatedLineEdit *pUiProxyIp, uint16_t nProxyPort);
71+
void quitOnReset();
7172

7273
private:
7374
Ui::OptionsDialog *ui;

0 commit comments

Comments
 (0)