QQmlDebugProcess: Remove cruft

The mutex had no function I can discern as the class is not thread safe
in any way, and isn't used from multiple threads anywhere. Extra event
loops are famously brittle. We don't have to use an extra event loop
here. QTest::qWaitFor will do just fine.

Task-number: QTBUG-101678
Change-Id: If25a961312a1eeb725ff779e386b2657e0f23dd1
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2022-03-15 11:18:13 +01:00
parent b236ad109d
commit 7b8e6714e1
2 changed files with 17 additions and 31 deletions

View File

@ -33,28 +33,23 @@
#include <QtCore/qdir.h>
#include <QtCore/qregularexpression.h>
#include <QtTest/qtest.h>
QQmlDebugProcess::QQmlDebugProcess(const QString &executable, QObject *parent)
: QObject(parent)
, m_executable(executable)
, m_state(SessionUnknown)
, m_port(0)
, m_maximumBindErrors(0)
, m_receivedBindErrors(0)
{
m_process.setProcessChannelMode(QProcess::MergedChannels);
m_timer.setInterval(15000);
connect(&m_process, &QProcess::readyReadStandardOutput,
this, &QQmlDebugProcess::processAppOutput);
connect(&m_process, &QProcess::errorOccurred,
this, &QQmlDebugProcess::processError);
connect(&m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this]() {
m_timer.stop();
m_eventLoop.quit();
if (m_state == SessionUnknown)
m_state = SessionFailed;
emit finished();
});
connect(&m_timer, &QTimer::timeout,
this, &QQmlDebugProcess::timeout);
}
QQmlDebugProcess::~QQmlDebugProcess()
@ -97,16 +92,13 @@ void QQmlDebugProcess::start(const QStringList &arguments)
}
}
#endif
m_mutex.lock();
m_port = 0;
m_process.setEnvironment(QProcess::systemEnvironment() + m_environment);
m_process.start(m_executable, arguments);
if (!m_process.waitForStarted()) {
qWarning() << "QML Debug Client: Could not launch app " << m_executable
<< ": " << m_process.errorString();
m_eventLoop.quit();
}
m_mutex.unlock();
}
void QQmlDebugProcess::stop()
@ -141,8 +133,11 @@ bool QQmlDebugProcess::waitForSessionStart()
return false;
}
m_timer.start();
m_eventLoop.exec();
if (!QTest::qWaitFor(
[this]() { return m_state == SessionFailed || m_state == SessionStarted; },
15000)) {
timeout();
}
return m_state == SessionStarted;
}
@ -179,8 +174,6 @@ QString QQmlDebugProcess::output() const
void QQmlDebugProcess::processAppOutput()
{
m_mutex.lock();
bool outputFromAppItself = false;
QString newOutput = m_process.readAll();
@ -198,18 +191,14 @@ void QQmlDebugProcess::processAppOutput()
auto portRx = QRegularExpression("Waiting for connection on port (\\d+)").match(line);
if (portRx.hasMatch()) {
m_port = portRx.captured(1).toInt();
m_timer.stop();
m_state = SessionStarted;
m_eventLoop.quit();
continue;
}
if (line.contains("Unable to listen")) {
if (++m_receivedBindErrors >= m_maximumBindErrors) {
if (m_maximumBindErrors == 0)
qWarning() << "App was unable to bind to port!";
m_timer.stop();
m_state = SessionFailed;
m_eventLoop.quit();
}
continue;
}
@ -218,7 +207,6 @@ void QQmlDebugProcess::processAppOutput()
// set to true if there is output not coming from the debugger or we don't understand it
outputFromAppItself = true;
}
m_mutex.unlock();
if (outputFromAppItself)
emit readyReadStandardOutput();

View File

@ -68,12 +68,11 @@ public:
void stop();
void setMaximumBindErrors(int numErrors);
signals:
Q_SIGNALS:
void readyReadStandardOutput();
void finished();
private slots:
void timeout();
private Q_SLOTS:
void processAppOutput();
void processError(QProcess::ProcessError error);
@ -84,18 +83,17 @@ private:
SessionFailed
};
void timeout();
QString m_executable;
QProcess m_process;
QString m_outputBuffer;
QString m_output;
QTimer m_timer;
QEventLoop m_eventLoop;
QMutex m_mutex;
SessionState m_state;
SessionState m_state = SessionUnknown;
QStringList m_environment;
int m_port;
int m_maximumBindErrors;
int m_receivedBindErrors;
int m_port = 0;
int m_maximumBindErrors = 0;
int m_receivedBindErrors = 0;
};
#endif // QQMLDEBUGPROCESS_P_H