QtcProcess: Fix terminate() for process launcher

ProcessLauncherImpl::terminate() was behaving the same as
ProcessLauncherImpl::kill() in case of process launcher
implementation - in both cases the launcher
immediately returned confirmation about receiving the
close request and was putting the corresponding
process into its reaper. However, the issue with this approach
is that we can't receive anymore any potential ready
read signal from the process being terminated after
a call to terminate().

The fix is to diverge the behavior of terminate()
and kill() of ProcessLauncherImpl. The behavior of kill()
remains the same, while terminate() just instructs the
process launcher to start termination without putting
the process into the reaper, yet.

Add a test that checks for any possible zombies.

We run the RecursiveBlockingProcess recursively. The most
nested process blocks. After a short wait we terminate
the outermost process. With this test we are trying to see
if terminating the middle process terminates also its
children and doesn't leave zombies.

Before we execute the test (and also by the end of this test)
we call Singleton::deleteAll() in order to ensure that reaping
of previously running processes has finished. We ensure the
number of running processtestapps is zero. Later, we ensure that
the leaf process was already started and the number of running
processtestapps equals the depth of recursion.

Fix apparent bug in getLocalProcessesUsingProc().

Change-Id: I7e2bc46ad5ca22f26620da86fbaf0fa00a7db3c3
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2022-04-01 15:14:05 +02:00
parent fea22eadb6
commit 00acccfd3d
10 changed files with 172 additions and 35 deletions

View File

@@ -114,12 +114,14 @@ StopProcessPacket::StopProcessPacket(quintptr token)
void StopProcessPacket::doSerialize(QDataStream &stream) const void StopProcessPacket::doSerialize(QDataStream &stream) const
{ {
Q_UNUSED(stream); stream << int(signalType);
} }
void StopProcessPacket::doDeserialize(QDataStream &stream) void StopProcessPacket::doDeserialize(QDataStream &stream)
{ {
Q_UNUSED(stream); int sig;
stream >> sig;
signalType = SignalType(sig);
} }
void WritePacket::doSerialize(QDataStream &stream) const void WritePacket::doSerialize(QDataStream &stream) const

View File

@@ -142,6 +142,13 @@ class StopProcessPacket : public LauncherPacket
public: public:
StopProcessPacket(quintptr token); StopProcessPacket(quintptr token);
enum class SignalType {
Kill,
Terminate
};
SignalType signalType = SignalType::Kill;
private: private:
void doSerialize(QDataStream &stream) const override; void doSerialize(QDataStream &stream) const override;
void doDeserialize(QDataStream &stream) override; void doDeserialize(QDataStream &stream) override;

View File

@@ -308,25 +308,44 @@ QProcess::ProcessState CallerHandle::state() const
return m_processState; return m_processState;
} }
void CallerHandle::cancel() bool CallerHandle::isStartPacketAwaitingAndClear()
{ {
QTC_ASSERT(isCalledFromCallersThread(), return); QMutexLocker locker(&m_mutex);
switch (m_processState.exchange(QProcess::NotRunning)) { const bool startPacketExisted = m_startPacket.get();
case QProcess::NotRunning: m_startPacket.reset();
break; return startPacketExisted;
case QProcess::Starting: }
void CallerHandle::sendStopPacket(StopProcessPacket::SignalType signalType)
{
if (m_processState == QProcess::NotRunning)
return;
if (m_processState == QProcess::Running || !isStartPacketAwaitingAndClear()) {
StopProcessPacket packet(m_token);
packet.signalType = signalType;
sendPacket(packet);
return;
}
m_processState.store(QProcess::NotRunning);
m_result.m_errorString = QCoreApplication::translate("Utils::LauncherHandle", m_result.m_errorString = QCoreApplication::translate("Utils::LauncherHandle",
"Process was canceled before it was started."); "Process was canceled before it was started.");
m_result.m_error = QProcess::FailedToStart; m_result.m_error = QProcess::FailedToStart;
if (LauncherInterface::isReady()) // TODO: race condition with m_processState???
sendPacket(StopProcessPacket(m_token));
else
emit errorOccurred(m_result.m_error); emit errorOccurred(m_result.m_error);
break; }
case QProcess::Running:
sendPacket(StopProcessPacket(m_token));
break; void CallerHandle::terminate()
} {
QTC_ASSERT(isCalledFromCallersThread(), return);
sendStopPacket(StopProcessPacket::SignalType::Terminate);
}
void CallerHandle::kill()
{
QTC_ASSERT(isCalledFromCallersThread(), return);
sendStopPacket(StopProcessPacket::SignalType::Kill);
} }
QByteArray CallerHandle::readAllStandardOutput() QByteArray CallerHandle::readAllStandardOutput()
@@ -416,7 +435,7 @@ void CallerHandle::doStart()
if (!m_startPacket) if (!m_startPacket)
return; return;
sendPacket(*m_startPacket); sendPacket(*m_startPacket);
m_startPacket.reset(nullptr); m_startPacket.reset();
} }
// Called from caller's or launcher's thread. // Called from caller's or launcher's thread.

View File

@@ -90,7 +90,10 @@ public:
// Called from caller's or launcher's thread. // Called from caller's or launcher's thread.
QProcess::ProcessState state() const; QProcess::ProcessState state() const;
void cancel(); bool isStartPacketAwaitingAndClear();
void sendStopPacket(StopProcessPacket::SignalType signalType);
void terminate();
void kill();
QByteArray readAllStandardOutput(); QByteArray readAllStandardOutput();
QByteArray readAllStandardError(); QByteArray readAllStandardError();

View File

@@ -98,7 +98,7 @@ static QList<ProcessInfo> getLocalProcessesUsingProc()
if (proc.executable.isEmpty()) { if (proc.executable.isEmpty()) {
QFile statFile(root + QLatin1String("/stat")); QFile statFile(root + QLatin1String("/stat"));
if (!statFile.open(QIODevice::ReadOnly)) { if (statFile.open(QIODevice::ReadOnly)) {
const QStringList data = QString::fromLocal8Bit(statFile.readAll()).split(QLatin1Char(' ')); const QStringList data = QString::fromLocal8Bit(statFile.readAll()).split(QLatin1Char(' '));
if (data.size() < 2) if (data.size() < 2)
continue; continue;

View File

@@ -422,7 +422,7 @@ public:
} }
~ProcessLauncherImpl() final ~ProcessLauncherImpl() final
{ {
cancel(); m_handle->kill();
LauncherInterface::unregisterHandle(token()); LauncherInterface::unregisterHandle(token());
m_handle = nullptr; m_handle = nullptr;
} }
@@ -435,9 +435,9 @@ public:
if (m_setup->m_useCtrlCStub) // bypass launcher and interrupt directly if (m_setup->m_useCtrlCStub) // bypass launcher and interrupt directly
ProcessHelper::interruptPid(processId()); ProcessHelper::interruptPid(processId());
} }
void terminate() final { cancel(); } // TODO: what are differences among terminate, kill and close? void terminate() final { m_handle->terminate(); }
void kill() final { cancel(); } // TODO: see above void kill() final { m_handle->kill(); }
void close() final { cancel(); } // TODO: see above void close() final { m_handle->kill(); } // TODO: is it more like terminate or kill?
qint64 write(const QByteArray &data) final { return m_handle->write(data); } qint64 write(const QByteArray &data) final { return m_handle->write(data); }
ProcessResultData resultData() const final { return m_handle->resultData(); }; ProcessResultData resultData() const final { return m_handle->resultData(); };
@@ -456,8 +456,6 @@ private:
m_handle->start(program, arguments); m_handle->start(program, arguments);
} }
void cancel();
quintptr token() const { return m_token; } quintptr token() const { return m_token; }
const uint m_token = 0; const uint m_token = 0;
@@ -465,11 +463,6 @@ private:
CallerHandle *m_handle = nullptr; CallerHandle *m_handle = nullptr;
}; };
void ProcessLauncherImpl::cancel()
{
m_handle->cancel();
}
static ProcessImpl defaultProcessImpl() static ProcessImpl defaultProcessImpl()
{ {
if (qEnvironmentVariableIsSet("QTC_USE_QPROCESS")) if (qEnvironmentVariableIsSet("QTC_USE_QPROCESS"))

View File

@@ -247,6 +247,15 @@ void LauncherSocketHandler::handleStopPacket()
logDebug("Got stop request for unknown process"); logDebug("Got stop request for unknown process");
return; return;
} }
const auto packet = LauncherPacket::extractPacket<StopProcessPacket>(
m_packetParser.token(),
m_packetParser.packetData());
if (packet.signalType == StopProcessPacket::SignalType::Terminate) {
process->terminate();
return;
}
if (process->state() == QProcess::NotRunning) { if (process->state() == QProcess::NotRunning) {
// This shouldn't happen, since as soon as process finishes or error occurrs // This shouldn't happen, since as soon as process finishes or error occurrs
// the process is being removed. // the process is being removed.

View File

@@ -38,6 +38,10 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#else
#include <atomic>
#include <signal.h>
#include <unistd.h>
#endif #endif
using namespace Utils; using namespace Utils;
@@ -210,7 +214,7 @@ int ProcessTestApp::CrashAfterOneSecond::main()
int ProcessTestApp::RecursiveCrashingProcess::main() int ProcessTestApp::RecursiveCrashingProcess::main()
{ {
const int currentDepth = qEnvironmentVariableIntValue(envVar()); const int currentDepth = qEnvironmentVariableIntValue(envVar());
if (currentDepth == 0) { if (currentDepth == 1) {
QThread::sleep(1); QThread::sleep(1);
doCrash(); doCrash();
return 1; return 1;
@@ -224,3 +228,56 @@ int ProcessTestApp::RecursiveCrashingProcess::main()
return process.exitCode(); return process.exitCode();
return s_crashCode; return s_crashCode;
} }
#ifndef Q_OS_WIN
static std::atomic_bool s_terminate = false;
void terminate(int signum)
{
Q_UNUSED(signum)
s_terminate.store(true);
}
#endif
int ProcessTestApp::RecursiveBlockingProcess::main()
{
#ifndef Q_OS_WIN
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = terminate;
sigaction(SIGTERM, &action, NULL);
#endif
const int currentDepth = qEnvironmentVariableIntValue(envVar());
if (currentDepth == 1) {
std::cout << s_leafProcessStarted << std::flush;
while (true) {
QThread::sleep(1);
#ifndef Q_OS_WIN
if (s_terminate.load()) {
std::cout << s_leafProcessTerminated << std::flush;
return s_crashCode;
}
#endif
}
}
SubProcessConfig subConfig(envVar(), QString::number(currentDepth - 1));
QtcProcess process;
subConfig.setupSubProcess(&process);
process.setProcessChannelMode(QProcess::ForwardedChannels);
process.start();
while (true) {
if (process.waitForFinished(1000))
return 0;
#ifndef Q_OS_WIN
if (s_terminate.load()) {
process.terminate();
process.waitForFinished(-1);
break;
}
#endif
}
if (process.exitStatus() == QProcess::NormalExit)
return process.exitCode();
return s_crashCode;
}

View File

@@ -71,6 +71,7 @@ public:
SUB_PROCESS(EmitOneErrorOnCrash); SUB_PROCESS(EmitOneErrorOnCrash);
SUB_PROCESS(CrashAfterOneSecond); SUB_PROCESS(CrashAfterOneSecond);
SUB_PROCESS(RecursiveCrashingProcess); SUB_PROCESS(RecursiveCrashingProcess);
SUB_PROCESS(RecursiveBlockingProcess);
// In order to get a value associated with the certain subprocess use SubProcessClass::envVar(). // In order to get a value associated with the certain subprocess use SubProcessClass::envVar().
// The classes above define different custom executables. Inside invokeSubProcess(), called // The classes above define different custom executables. Inside invokeSubProcess(), called
@@ -119,5 +120,7 @@ enum class BlockType {
}; };
static const int s_crashCode = 123; static const int s_crashCode = 123;
static const char s_leafProcessStarted[] = "Leaf process started";
static const char s_leafProcessTerminated[] = "Leaf process terminated";
Q_DECLARE_METATYPE(BlockType) Q_DECLARE_METATYPE(BlockType)

View File

@@ -31,6 +31,7 @@
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/launcherinterface.h> #include <utils/launcherinterface.h>
#include <utils/porting.h> #include <utils/porting.h>
#include <utils/processinfo.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/singleton.h> #include <utils/singleton.h>
@@ -162,6 +163,7 @@ private slots:
void emitOneErrorOnCrash(); void emitOneErrorOnCrash();
void crashAfterOneSecond(); void crashAfterOneSecond();
void recursiveCrashingProcess(); void recursiveCrashingProcess();
void recursiveBlockingProcess();
void cleanupTestCase(); void cleanupTestCase();
@@ -1187,7 +1189,7 @@ void tst_QtcProcess::crashAfterOneSecond()
void tst_QtcProcess::recursiveCrashingProcess() void tst_QtcProcess::recursiveCrashingProcess()
{ {
const int recursionDepth = 5; // must be at least 1 const int recursionDepth = 5; // must be at least 2
SubProcessConfig subConfig(ProcessTestApp::RecursiveCrashingProcess::envVar(), SubProcessConfig subConfig(ProcessTestApp::RecursiveCrashingProcess::envVar(),
QString::number(recursionDepth)); QString::number(recursionDepth));
TestProcess process; TestProcess process;
@@ -1199,6 +1201,48 @@ void tst_QtcProcess::recursiveCrashingProcess()
QCOMPARE(process.exitStatus(), QProcess::NormalExit); QCOMPARE(process.exitStatus(), QProcess::NormalExit);
QCOMPARE(process.exitCode(), s_crashCode); QCOMPARE(process.exitCode(), s_crashCode);
} }
static int runningTestProcessCount()
{
int testProcessCounter = 0;
const QList<ProcessInfo> processInfoList = ProcessInfo::processInfoList();
for (const ProcessInfo &processInfo : processInfoList) {
if (FilePath::fromString(processInfo.executable).baseName() == "processtestapp")
++testProcessCounter;
}
return testProcessCounter;
}
void tst_QtcProcess::recursiveBlockingProcess()
{
if (HostOsInfo::isWindowsHost())
QSKIP("Windows implementation of this test is lacking handling of WM_CLOSE message.");
Singleton::deleteAll();
QCOMPARE(runningTestProcessCount(), 0);
const int recursionDepth = 5; // must be at least 2
SubProcessConfig subConfig(ProcessTestApp::RecursiveBlockingProcess::envVar(),
QString::number(recursionDepth));
{
TestProcess process;
subConfig.setupSubProcess(&process);
process.start();
QVERIFY(process.waitForStarted(1000));
QVERIFY(process.waitForReadyRead(1000));
QCOMPARE(process.readAllStandardOutput(), s_leafProcessStarted);
QCOMPARE(runningTestProcessCount(), recursionDepth);
QVERIFY(!process.waitForFinished(1000));
process.terminate();
QVERIFY(process.waitForReadyRead());
QCOMPARE(process.readAllStandardOutput(), s_leafProcessTerminated);
QVERIFY(process.waitForFinished());
QCOMPARE(process.exitStatus(), QProcess::NormalExit);
QCOMPARE(process.exitCode(), s_crashCode);
}
Singleton::deleteAll();
QCOMPARE(runningTestProcessCount(), 0);
}
QTEST_MAIN(tst_QtcProcess) QTEST_MAIN(tst_QtcProcess)
#include "tst_qtcprocess.moc" #include "tst_qtcprocess.moc"