Skip to content

Commit

Permalink
Allow for process termination when polling with isRunning
Browse files Browse the repository at this point in the history
On *NIX, one needs to call `waitpid()` in order for process to exit the
zombie state. If one uses `Process::isRunning()` to emulate non-blocking
wait for child process termination, process will stay zombie and function
will always return true.

This commit changes `Process::isRunning()` to call `waitpid()` with
`WNOHANG` instead of using `kill()` when checking for child process (i.e.
the one we have ProcessHandle for), which allows for process termination.
Additional trickery with mutex and event is needed to prevent exceptions
when `Process::isRunning()` and/or `Process::wait()` is called concurrently
on the same handle from different threads.

Fixes #1097.
  • Loading branch information
mikedld committed Jan 8, 2016
1 parent 279ea9d commit 0425866
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
9 changes: 8 additions & 1 deletion Foundation/include/Poco/Process_UNIX.h
Expand Up @@ -21,6 +21,9 @@


#include "Poco/Foundation.h"
#include "Poco/Event.h"
#include "Poco/Mutex.h"
#include "Poco/Optional.h"
#include "Poco/RefCountedObject.h"
#include <unistd.h>
#include <vector>
Expand All @@ -41,9 +44,13 @@ class Foundation_API ProcessHandleImpl: public RefCountedObject

pid_t id() const;
int wait() const;
int wait(int options) const;

private:
pid_t _pid;
const pid_t _pid;
mutable FastMutex _mutex;
mutable Event _event;
mutable Optional<int> _status;
};


Expand Down
60 changes: 50 additions & 10 deletions Foundation/src/Process_UNIX.cpp
Expand Up @@ -18,6 +18,7 @@
#include "Poco/Exception.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Pipe.h"
#include "Poco/Thread.h"
#include <limits>
#include <errno.h>
#include <signal.h>
Expand All @@ -42,7 +43,10 @@ namespace Poco {
// ProcessHandleImpl
//
ProcessHandleImpl::ProcessHandleImpl(pid_t pid):
_pid(pid)
_pid(pid),
_mutex(),
_event(Event::EVENT_MANUALRESET),
_status()
{
}

Expand All @@ -60,24 +64,60 @@ pid_t ProcessHandleImpl::id() const

int ProcessHandleImpl::wait() const
{
int status;
int rc;
do
{
rc = waitpid(_pid, &status, 0);
}
while (rc < 0 && errno == EINTR);
if (rc != _pid)
if (wait(0) != _pid)
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));

const int status = _status.value();
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSIGNALED(status))
return -WTERMSIG(status);

// This line should never be reached.
return std::numeric_limits<int>::max();
}


int ProcessHandleImpl::wait(int options) const
{
{
FastMutex::ScopedLock lock(_mutex);
if (_status.isSpecified())
{
return _pid;
}
}

int status;
int rc;
do
{
rc = waitpid(_pid, &status, options);
}
while (rc < 0 && errno == EINTR);

if (rc == _pid)
{
FastMutex::ScopedLock lock(_mutex);
_status = status;
_event.set();
}
else if (rc < 0 && errno == ECHILD)
{
// Looks like another thread was lucky and it should update the status for us shortly
_event.wait();

FastMutex::ScopedLock lock(_mutex);
if (_status.isSpecified())
{
rc = _pid;
}
}

return rc;
}


//
// ProcessImpl
//
Expand Down Expand Up @@ -251,7 +291,7 @@ void ProcessImpl::killImpl(PIDImpl pid)

bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
return isRunningImpl(handle.id());
return handle.wait(WNOHANG) == 0;
}


Expand Down
23 changes: 23 additions & 0 deletions Foundation/testsuite/src/ProcessTest.cpp
Expand Up @@ -16,6 +16,7 @@
#include "Poco/Process.h"
#include "Poco/Pipe.h"
#include "Poco/PipeStream.h"
#include "Poco/Thread.h"
#include <csignal>


Expand Down Expand Up @@ -176,6 +177,27 @@ void ProcessTest::testIsRunning()
}


void ProcessTest::testIsRunningAllowsForTermination()
{
#if !defined(_WIN32_WCE)
std::string name("TestApp");
std::string cmd;

#if defined(POCO_OS_FAMILY_UNIX)
cmd = "./";
cmd += name;
#else
cmd = name;
#endif

std::vector<std::string> args;
ProcessHandle ph = Process::launch(cmd, args, 0, 0, 0);
while (Process::isRunning(ph))
Poco::Thread::sleep(100);
#endif // !defined(_WIN32_WCE)
}


void ProcessTest::testSignalExitCode()
{
#if defined(POCO_OS_FAMILY_UNIX)
Expand Down Expand Up @@ -213,6 +235,7 @@ CppUnit::Test* ProcessTest::suite()
CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectOut);
CppUnit_addTest(pSuite, ProcessTest, testLaunchEnv);
CppUnit_addTest(pSuite, ProcessTest, testIsRunning);
CppUnit_addTest(pSuite, ProcessTest, testIsRunningAllowsForTermination);
CppUnit_addTest(pSuite, ProcessTest, testSignalExitCode);

return pSuite;
Expand Down
1 change: 1 addition & 0 deletions Foundation/testsuite/src/ProcessTest.h
Expand Up @@ -31,6 +31,7 @@ class ProcessTest: public CppUnit::TestCase
void testLaunchRedirectOut();
void testLaunchEnv();
void testIsRunning();
void testIsRunningAllowsForTermination();
void testSignalExitCode();

void setUp();
Expand Down

0 comments on commit 0425866

Please sign in to comment.