Skip to content

Commit

Permalink
Fix client crash on startup for Windows with fast CPUs
Browse files Browse the repository at this point in the history
Also includes other init cleanups.
  • Loading branch information
jamescowens committed Dec 10, 2019
1 parent 1c3568b commit c9e5eac
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/global_objects_noui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern int nBoincUtilization;
extern std::string sRegVer;
extern bool bForceUpdate;
extern bool fQtActive;
extern bool bGridcoinGUILoaded;
extern bool bGridcoinCoreInitComplete;

// Timers
extern std::map<std::string, int> mvTimers; // Contains event timers that reset after max ms duration iterator is exceeded
Expand Down
38 changes: 20 additions & 18 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,13 @@ void InitLogging()
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
LogPrintf("Using data directory %s\n", GetDataDir().string());

std::string version_string = FormatFullVersion();
std::string build_type;
#ifdef DEBUG
version_string += " (debug build)";
build_type = "debug build";
#else
version_string += " (release build)";
build_type = "release build";
#endif
LogPrintf(PACKAGE_NAME " version %s\n", version_string);
LogPrintf(PACKAGE_NAME " version %s (%s - %s)", FormatFullVersion(), build_type, CLIENT_DATE);
}


Expand All @@ -428,11 +428,16 @@ void ThreadAppInit2(ThreadHandlerPtr th)
{
// Make this thread recognisable
RenameThread("grc-appinit2");
bGridcoinGUILoaded=false;
LogPrintf("Initializing GUI...");

bGridcoinCoreInitComplete = false;

LogPrintf("Initializing Core...");

AppInit2(th);
LogPrintf("GUI Loaded...");
bGridcoinGUILoaded = true;

LogPrintf("Core Initialized...");

bGridcoinCoreInitComplete = true;
}


Expand Down Expand Up @@ -733,7 +738,6 @@ bool AppInit2(ThreadHandlerPtr threads)
}
#endif

LogPrintf("Gridcoin version %s (%s)", FormatFullVersion(), CLIENT_DATE);
LogPrintf("Using OpenSSL version %s", SSLeay_version(SSLEAY_VERSION));
if (!fLogTimestamps)
LogPrintf("Startup time: %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()));
Expand All @@ -756,15 +760,6 @@ bool AppInit2(ThreadHandlerPtr threads)

int64_t nStart;


// Start the lightweight task scheduler thread
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler);
threadGroup.create_thread(std::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));

// TODO: Do we need this? It would require porting the Bitcoin signal handler.
// GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);


// ********************************************************* Step 5: verify database integrity

uiInterface.InitMessage(_("Verifying database integrity..."));
Expand Down Expand Up @@ -1180,6 +1175,13 @@ bool AppInit2(ThreadHandlerPtr threads)
int64_t nBalanceInQuestion;
pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion);

// Start the lightweight task scheduler thread
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler);
threadGroup.create_thread(std::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));

// TODO: Do we need this? It would require porting the Bitcoin signal handler.
// GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);

scheduler.scheduleEvery([]{
g_banman->DumpBanlist();
}, DUMP_BANS_INTERVAL * 1000);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ BlockFinder blockFinder;

bool bForceUpdate = false;
bool fQtActive = false;
bool bGridcoinGUILoaded = false;
bool bGridcoinCoreInitComplete = false;

extern void GetGlobalStatus();
bool PollIsActive(const std::string& poll_contract);
Expand Down
23 changes: 15 additions & 8 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static std::string Translate(const char* psz)
void DebugMessageHandler(QtMsgType type, const char *msg)
{
if (type == QtDebugMsg) {
LogPrint("qt", "GUI: %s\n", msg);
LogPrint(BCLog::LogFlags::QT, "GUI: %s\n", msg);
} else {
LogPrintf("GUI: %s\n", msg);
}
Expand All @@ -181,7 +181,7 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons
{
Q_UNUSED(context);
if (type == QtDebugMsg) {
LogPrint("qt", "GUI: %s\n", msg.toStdString());
LogPrint(BCLog::LogFlags::QT, "GUI: %s\n", msg.toStdString());
} else {
LogPrintf("GUI: %s\n", msg.toStdString());
}
Expand Down Expand Up @@ -333,14 +333,15 @@ int StartGridcoinQt(int argc, char *argv[])
// Install qDebug() message handler to route to debug.log
qInstallMsgHandler(DebugMessageHandler);
#else
#if defined(WIN32)
// Install global event filter for processing Windows session related Windows messages (WM_QUERYENDSESSION and WM_ENDSESSION)
qApp->installNativeEventFilter(new WinShutdownMonitor());
#endif
// Install qDebug() message handler to route to debug.log
qInstallMessageHandler(DebugMessageHandler);
#endif

#if defined(WIN32) && QT_VERSION >= 0x050000
// Install global event filter for processing Windows session related Windows messages (WM_QUERYENDSESSION and WM_ENDSESSION)
app.installNativeEventFilter(new WinShutdownMonitor());
#endif

if (!boost::filesystem::is_directory(GetDataDir(false)))
{
QMessageBox::critical(0, "Gridcoin",
Expand Down Expand Up @@ -442,10 +443,14 @@ int StartGridcoinQt(int argc, char *argv[])
else
{
//10-31-2015
while (!bGridcoinGUILoaded)
while (!bGridcoinCoreInitComplete)
{
app.processEvents();
MilliSleep(300);

// The sleep here has to be pretty short to avoid a buffer overflow crash with
// fast CPUs due to too many events. It originally was set to 300 ms and has
// been reduced to 100 ms.
MilliSleep(100);
}

{
Expand Down Expand Up @@ -479,6 +484,8 @@ int StartGridcoinQt(int argc, char *argv[])
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely...").arg(QObject::tr(PACKAGE_NAME)), (HWND)window.winId());
#endif

LogPrintf("GUI loaded.");

app.exec();

window.hide();
Expand Down
12 changes: 6 additions & 6 deletions src/qt/winshutdownmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pM

// Seed OpenSSL PRNG with Windows event data (e.g. mouse movements and other user interactions)
if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
// Warn only once as this is performance-critical
static bool warned = false;
if (!warned) {
LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
warned = true;
}
// Warn only once as this is performance-critical
static bool warned = false;
if (!warned) {
LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
warned = true;
}
}

switch(pMsg->message)
Expand Down
5 changes: 5 additions & 0 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,12 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
return nLoadWalletRet;
fFirstRunRet = !vchDefaultKey.IsValid();

LogPrintf("LoadWallet: Starting new wallet flush thread.");

NewThread(ThreadFlushWalletDB, &strWalletFile);

LogPrintf("LoadWallet: New wallet flush thread started.");

return DB_LOAD_OK;
}

Expand Down

0 comments on commit c9e5eac

Please sign in to comment.