Skip to content

Commit 09e84de

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

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>
@@ -259,6 +260,7 @@ void BitcoinApplication::createOptionsModel(bool resetSettings)
259260
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
260261
{
261262
window = new BitcoinGUI(node(), platformStyle, networkStyle, nullptr);
263+
connect(window, &BitcoinGUI::quitClicked, this, &BitcoinApplication::requestShutdown);
262264

263265
pollShutdownTimer = new QTimer(window);
264266
connect(pollShutdownTimer, &QTimer::timeout, window, &BitcoinGUI::detectShutdown);
@@ -333,14 +335,18 @@ void BitcoinApplication::requestInitialize()
333335

334336
void BitcoinApplication::requestShutdown()
335337
{
338+
for (const auto w : QGuiApplication::topLevelWindows()) {
339+
w->hide();
340+
}
341+
336342
// Show a simple window indicating shutdown status
337343
// Do this first as some of the steps may take some time below,
338344
// for example the RPC console may still be executing a command.
339345
shutdownWindow.reset(ShutdownWindow::showShutdownWindow(window));
340346

341347
qDebug() << __func__ << ": Requesting shutdown";
342348
startThread();
343-
window->hide();
349+
344350
// Must disconnect node signals otherwise current thread can deadlock since
345351
// no event loop is running.
346352
window->unsubscribeFromCoreSignals();
@@ -406,13 +412,13 @@ void BitcoinApplication::initializeResult(bool success, interfaces::BlockAndHead
406412
pollShutdownTimer->start(200);
407413
} else {
408414
Q_EMIT splashFinished(); // Make sure splash screen doesn't stick around during shutdown
409-
quit(); // Exit first main loop invocation
415+
requestShutdown();
410416
}
411417
}
412418

413419
void BitcoinApplication::shutdownResult()
414420
{
415-
quit(); // Exit second main loop invocation after shutdown finished
421+
quit();
416422
}
417423

418424
void BitcoinApplication::handleRunawayException(const QString &message)
@@ -635,8 +641,6 @@ int GuiMain(int argc, char* argv[])
635641
#if defined(Q_OS_WIN)
636642
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely…").arg(PACKAGE_NAME), (HWND)app.getMainWinId());
637643
#endif
638-
app.exec();
639-
app.requestShutdown();
640644
app.exec();
641645
rv = app.getReturnValue();
642646
} else {

src/qt/bitcoin.h

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

7979
/// Request core initialization
8080
void requestInitialize();
81-
/// Request core shutdown
82-
void requestShutdown();
8381

8482
/// Get process return value
8583
int getReturnValue() const { return returnValue; }
@@ -95,6 +93,8 @@ class BitcoinApplication: public QApplication
9593

9694
public Q_SLOTS:
9795
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
96+
/// Request core shutdown
97+
void requestShutdown();
9898
void shutdownResult();
9999
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
100100
void handleRunawayException(const QString &message);

src/qt/bitcoingui.cpp

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

372-
connect(quitAction, &QAction::triggered, qApp, QApplication::quit);
372+
connect(quitAction, &QAction::triggered, this, &BitcoinGUI::quitClicked);
373373
connect(aboutAction, &QAction::triggered, this, &BitcoinGUI::aboutClicked);
374374
connect(aboutQtAction, &QAction::triggered, qApp, QApplication::aboutQt);
375375
connect(optionsAction, &QAction::triggered, this, &BitcoinGUI::optionsClicked);
@@ -954,6 +954,7 @@ void BitcoinGUI::openOptionsDialogWithTab(OptionsDialog::Tab tab)
954954
return;
955955

956956
auto dlg = new OptionsDialog(this, enableWallet);
957+
connect(dlg, &OptionsDialog::quitOnReset, this, &BitcoinGUI::quitClicked);
957958
dlg->setCurrentTab(tab);
958959
dlg->setModel(clientModel->getOptionsModel());
959960
dlg->setAttribute(Qt::WA_DeleteOnClose);
@@ -1172,7 +1173,7 @@ void BitcoinGUI::closeEvent(QCloseEvent *event)
11721173
// close rpcConsole in case it was open to make some space for the shutdown window
11731174
rpcConsole->close();
11741175

1175-
QApplication::quit();
1176+
Q_EMIT quitClicked();
11761177
}
11771178
else
11781179
{
@@ -1367,7 +1368,7 @@ void BitcoinGUI::detectShutdown()
13671368
{
13681369
if(rpcConsole)
13691370
rpcConsole->hide();
1370-
qApp->quit();
1371+
Q_EMIT quitClicked();
13711372
}
13721373
}
13731374

src/qt/bitcoingui.h

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

213213
Q_SIGNALS:
214+
void quitClicked();
214215
/** Signal raised when a URI was entered or dragged to the GUI */
215216
void receivedURI(const QString &uri);
216217
/** Signal raised when RPC console shown */

src/qt/optionsdialog.cpp

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

283283
/* reset all options and close GUI */
284284
model->Reset();
285-
QApplication::quit();
285+
close();
286+
Q_EMIT quitOnReset();
286287
}
287288
}
288289

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)