Deviceshell: Cleanup shellProcess destruction

Change-Id: I71507d9e74979fca461784575257e7bafd1b4838
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Marcus Tillmanns
2022-09-14 09:52:09 +02:00
parent 720d07928b
commit 5ebb467cc6
5 changed files with 17 additions and 16 deletions

View File

@@ -187,12 +187,12 @@ DeviceShell::DeviceShell()
DeviceShell::~DeviceShell() DeviceShell::~DeviceShell()
{ {
m_shellProcess->deleteLater();
if (m_thread.isRunning()) { if (m_thread.isRunning()) {
m_thread.quit(); m_thread.quit();
m_thread.wait(); m_thread.wait();
} }
QTC_CHECK(!m_shellProcess);
} }
/*! /*!
@@ -247,7 +247,7 @@ DeviceShell::RunResult DeviceShell::run(const CommandLine &cmd, const QByteArray
const int id = ++m_currentId; const int id = ++m_currentId;
const auto it = m_commandOutput.insert(id, CommandRun{{-1, {}, {}}, &waiter}); const auto it = m_commandOutput.insert(id, CommandRun{{-1, {}, {}}, &waiter});
QMetaObject::invokeMethod(m_shellProcess, [this, id, cmd, stdInData]() { QMetaObject::invokeMethod(m_shellProcess.get(), [this, id, cmd, stdInData]() {
const QString command = QString("%1 \"%2\" %3\n") const QString command = QString("%1 \"%2\" %3\n")
.arg(id) .arg(id)
.arg(QString::fromLatin1(stdInData.toBase64())) .arg(QString::fromLatin1(stdInData.toBase64()))
@@ -302,13 +302,12 @@ void DeviceShell::startupFailed(const CommandLine &cmdLine)
*/ */
bool DeviceShell::start() bool DeviceShell::start()
{ {
m_shellProcess = new QtcProcess(); m_shellProcess = std::make_unique<QtcProcess>();
connect(m_shellProcess, &QtcProcess::done, m_shellProcess, connect(m_shellProcess.get(), &QtcProcess::done, m_shellProcess.get(),
[this] { emit done(m_shellProcess->resultData()); }); [this] { emit done(m_shellProcess->resultData()); });
connect(m_shellProcess, &QObject::destroyed, this, [this] { m_shellProcess = nullptr; }); connect(&m_thread, &QThread::finished, m_shellProcess.get(), [this] { closeShellProcess(); }, Qt::DirectConnection);
connect(&m_thread, &QThread::finished, m_shellProcess, [this] { closeShellProcess(); });
setupShellProcess(m_shellProcess); setupShellProcess(m_shellProcess.get());
m_shellProcess->setProcessMode(ProcessMode::Writer); m_shellProcess->setProcessMode(ProcessMode::Writer);
@@ -317,7 +316,7 @@ bool DeviceShell::start()
bool result = false; bool result = false;
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
m_shellProcess, m_shellProcess.get(),
[this] { [this] {
m_shellProcess->start(); m_shellProcess->start();
@@ -326,10 +325,10 @@ bool DeviceShell::start()
return false; return false;
} }
connect(m_shellProcess, &QtcProcess::readyReadStandardOutput, m_shellProcess, [this] { connect(m_shellProcess.get(), &QtcProcess::readyReadStandardOutput, m_shellProcess.get(), [this] {
onReadyRead(); onReadyRead();
}); });
connect(m_shellProcess, &QtcProcess::readyReadStandardError, m_shellProcess, [this] { connect(m_shellProcess.get(), &QtcProcess::readyReadStandardError, m_shellProcess.get(), [this] {
const QByteArray stdErr = m_shellProcess->readAllStandardError(); const QByteArray stdErr = m_shellProcess->readAllStandardError();
if (m_shellScriptState == State::Unknown) { if (m_shellScriptState == State::Unknown) {
@@ -347,7 +346,7 @@ bool DeviceShell::start()
qCWarning(deviceShellLog) << "Received unexpected output on stderr:" << stdErr; qCWarning(deviceShellLog) << "Received unexpected output on stderr:" << stdErr;
}); });
connect(m_shellProcess, &QtcProcess::done, m_shellProcess, [this]() { connect(m_shellProcess.get(), &QtcProcess::done, m_shellProcess.get(), [this] {
if (m_shellProcess->resultData().m_exitCode != EXIT_SUCCESS if (m_shellProcess->resultData().m_exitCode != EXIT_SUCCESS
|| m_shellProcess->resultData().m_exitStatus != QProcess::NormalExit) { || m_shellProcess->resultData().m_exitStatus != QProcess::NormalExit) {
qCWarning(deviceShellLog) << "Shell exited with error code:" qCWarning(deviceShellLog) << "Shell exited with error code:"
@@ -400,6 +399,7 @@ void DeviceShell::closeShellProcess()
if (!m_shellProcess->waitForFinished(2000)) if (!m_shellProcess->waitForFinished(2000))
m_shellProcess->terminate(); m_shellProcess->terminate();
} }
m_shellProcess.reset();
} }
} }

View File

@@ -96,7 +96,7 @@ private:
QWaitCondition *waiter; QWaitCondition *waiter;
}; };
QtcProcess *m_shellProcess = nullptr; std::unique_ptr<QtcProcess> m_shellProcess;
QThread m_thread; QThread m_thread;
int m_currentId{0}; int m_currentId{0};

View File

@@ -1027,6 +1027,7 @@ QtcProcess::QtcProcess(QObject *parent)
: QObject(parent), : QObject(parent),
d(new QtcProcessPrivate(this)) d(new QtcProcessPrivate(this))
{ {
qRegisterMetaType<ProcessResultData>("ProcessResultData");
static int qProcessExitStatusMeta = qRegisterMetaType<QProcess::ExitStatus>(); static int qProcessExitStatusMeta = qRegisterMetaType<QProcess::ExitStatus>();
static int qProcessProcessErrorMeta = qRegisterMetaType<QProcess::ProcessError>(); static int qProcessProcessErrorMeta = qRegisterMetaType<QProcess::ProcessError>();
Q_UNUSED(qProcessExitStatusMeta) Q_UNUSED(qProcessExitStatusMeta)

View File

@@ -454,7 +454,7 @@ void DockerDevicePrivate::startContainer()
return; return;
qCWarning(dockerDeviceLog) << "Container shell encountered error:" << resultData.m_error; qCWarning(dockerDeviceLog) << "Container shell encountered error:" << resultData.m_error;
m_shell.reset(); m_shell.release()->deleteLater();
DockerApi::recheckDockerDaemon(); DockerApi::recheckDockerDaemon();
MessageManager::writeFlashing(tr("Docker daemon appears to be not running. " MessageManager::writeFlashing(tr("Docker daemon appears to be not running. "

View File

@@ -814,7 +814,7 @@ public:
cmd.addArg("/bin/sh"); cmd.addArg("/bin/sh");
m_shell.reset(new LinuxDeviceShell(cmd)); m_shell.reset(new LinuxDeviceShell(cmd));
connect(m_shell.get(), &DeviceShell::done, this, [this] { m_shell.reset(); }); connect(m_shell.get(), &DeviceShell::done, this, [this] { m_shell.release()->deleteLater(); });
return m_shell->start(); return m_shell->start();
} }