poco icon indicating copy to clipboard operation
poco copied to clipboard

[question] Usage of `ProcessHandleImpl::wait()` in Unix implemention

Open HR1025 opened this issue 2 years ago • 1 comments

Some of the subprocesses from the fork() execute very quickly and complete before the waitpid(). In this situation poco will throw exception SystemException("Cannot wait for process").

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

	if (WIFEXITED(status)) // normal termination
		return WEXITSTATUS(status);
	else // termination by a signal
		return 256 + WTERMSIG(status);
}

But maybe we still want to get it exit code, show as follow:

        try
        {
            res = ph.wait();
            LOG_INFO << "wait pid " << ph.id() << ", exit code is: " << res;
        }
        catch(const Poco::Exception& ex)
        {
            LOG_INFO << "fork process already return, manually acquire it exit code";
            int status;
            waitpid(ph.id(), &status, WUNTRACED);
            res = WIFEXITED(status);
            LOG_INFO << "wait pid " << ph.id() << ", exit code is: " << res;
        }

I don't know how to do it. Is there a better way?

HR1025 avatar Mar 01 '23 03:03 HR1025

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Mar 01 '24 02:03 github-actions[bot]