Skip to content

Commit

Permalink
Terminate monitor process if it hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Feb 2, 2017
1 parent 9b6e7da commit 6d39919
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/app/remoteprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

RemoteProcess::RemoteProcess(QObject *parent)
: QObject(parent)
, m_process(NULL)
, m_pongRetry(false)
, m_state(Unconnected)
{
Expand All @@ -42,6 +43,7 @@ RemoteProcess::RemoteProcess(QObject *parent)

RemoteProcess::~RemoteProcess()
{
terminate();
m_timerPing.stop();
m_timerPongTimeout.stop();
}
Expand Down Expand Up @@ -73,16 +75,12 @@ void RemoteProcess::start(const QString &newServerName, const QStringList &argum
.arg(QCoreApplication::applicationFilePath())
.arg(arguments.join(" ")) );

qint64 monitorPid;
if ( !QProcess::startDetached(QCoreApplication::applicationFilePath(), arguments,
QString(), &monitorPid) )
{
log( "Remote process: Failed to start new remote process!", LogError );
onConnectionError();
return;
}

COPYQ_LOG( QString("Remote process: Started with pid %1.").arg(monitorPid) );
terminate();
m_process = new QProcess(this);
m_process->start(QCoreApplication::applicationFilePath(), arguments, QIODevice::NotOpen);
m_process->closeReadChannel(QProcess::StandardOutput);
m_process->closeReadChannel(QProcess::StandardError);
m_process->closeWriteChannel();

QTimer::singleShot(16000, this, SLOT(checkConnection()));
}
Expand Down Expand Up @@ -151,6 +149,21 @@ void RemoteProcess::onConnectionError()
emit connectionError();
}

void RemoteProcess::terminate()
{
if (!m_process)
return;

if (m_process->state() != QProcess::NotRunning) {
m_process->terminate();
if (!m_process->waitForFinished(100))
m_process->kill();
}

m_process->deleteLater();
m_process = NULL;
}

void RemoteProcess::writeMessage(const QByteArray &msg, int messageCode)
{
emit sendMessage(msg, messageCode);
Expand Down
4 changes: 4 additions & 0 deletions src/app/remoteprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class ClientSocket;
class Server;
class QByteArray;
class QProcess;
class QString;

/**
Expand Down Expand Up @@ -84,6 +85,9 @@ private slots:
void onConnectionError();

private:
void terminate();

QProcess *m_process;
QTimer m_timerPing;
QTimer m_timerPongTimeout;
bool m_pongRetry;
Expand Down

0 comments on commit 6d39919

Please sign in to comment.