Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DaemonManager: prevent GUI from hanging on hanging monerod launch #4244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/daemon/DaemonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void DaemonManager::stopAsync(NetworkType::Type nettype, const QString &dataDir,
{
const auto feature = m_scheduler.run([this, nettype, dataDir] {
QString message;
sendCommand({"exit"}, nettype, dataDir, message);
sendCommand({"exit"}, nettype, dataDir, message, /* no timeout */ -1);

return QJSValueList({stopWatcher(nettype, dataDir)});
}, callback);
Expand Down Expand Up @@ -239,9 +239,21 @@ void DaemonManager::printError()
bool DaemonManager::running(NetworkType::Type nettype, const QString &dataDir) const
{
QString status;
sendCommand({"sync_info"}, nettype, dataDir, status);
bool started = sendCommand(
{"sync_info"},
nettype,
dataDir,
status,
// milliseconds before the command
// timesout and returns `false`.
5000
);

qDebug() << status;
return status.contains("Height:");

// Did `monerod` start successfully
// and print out its height?
return started && status.contains("Height:");
}

bool DaemonManager::noSync() const noexcept
Expand All @@ -256,7 +268,12 @@ void DaemonManager::runningAsync(NetworkType::Type nettype, const QString &dataD
}, callback);
}

bool DaemonManager::sendCommand(const QStringList &cmd, NetworkType::Type nettype, const QString &dataDir, QString &message) const
// If launching `monerod` takes longer
// than `ms_timeout` amount of milliseconds,
// this function will fail and return `false`.
//
// Providing `-1` as the timeout means no timeout.
bool DaemonManager::sendCommand(const QStringList &cmd, NetworkType::Type nettype, const QString &dataDir, QString &message, const int ms_timeout) const
{
QProcess p;
QStringList external_cmd(cmd);
Expand All @@ -277,7 +294,14 @@ bool DaemonManager::sendCommand(const QStringList &cmd, NetworkType::Type nettyp

p.start(m_monerod, external_cmd);

bool started = p.waitForFinished(-1);
bool started = p.waitForFinished(ms_timeout);

// `monerod` either failed to start or
// took longer than `ms_timeout` to start.
if (!started) {
return false;
}

message = p.readAllStandardOutput();
emit daemonConsoleUpdated(message);
return started;
Expand All @@ -287,7 +311,7 @@ void DaemonManager::sendCommandAsync(const QStringList &cmd, NetworkType::Type n
{
m_scheduler.run([this, cmd, nettype, dataDir] {
QString message;
return QJSValueList({sendCommand(cmd, nettype, dataDir, message)});
return QJSValueList({sendCommand(cmd, nettype, dataDir, message, /* no timeout */ -1)});
}, callback);
}

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/DaemonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DaemonManager : public QObject
private:

bool running(NetworkType::Type nettype, const QString &dataDir) const;
bool sendCommand(const QStringList &cmd, NetworkType::Type nettype, const QString &dataDir, QString &message) const;
bool sendCommand(const QStringList &cmd, NetworkType::Type nettype, const QString &dataDir, QString &message, const int ms_timeout) const;
bool startWatcher(NetworkType::Type nettype, const QString &dataDir) const;
bool stopWatcher(NetworkType::Type nettype, const QString &dataDir) const;
signals:
Expand Down
Loading