Replies: 2 comments
-
This issue is stale because it has been open for 365 days with no activity. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@pavledragisic couldn't reliably reproduce #include "Poco/Process.h"
#include "Poco/Exception.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Logger.h"
#include "Poco/ConsoleChannel.h"
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <thread>
// Thread to reap the process early
void reapProcess(pid_t pid)
{
usleep(100 * 1000); // 100ms delay to ensure child process exits
int status;
waitpid(pid, &status, 0); // Reap the child process
}
int main()
{
Poco::AutoPtr<Poco::ConsoleChannel> consoleChannel(new Poco::ConsoleChannel());
Poco::Logger::root().setChannel(consoleChannel);
Poco::Logger& logger = Poco::Logger::get("ProcessLogger");
try
{
Poco::Process::Args args;
std::string command = "short-lived.sh"; // A command that exits immediately
Poco::ProcessHandle ph = Poco::Process::launch(command, args);
// Start a thread to reap the process
//std::thread reaper(reapProcess, ph.id());
// Simulate delay in parent process
logger.information("Simulating delay in parent process...");
sleep(60); // Allow child to exit and be reaped
int exitCode = -1;
try
{
exitCode = ph.wait(); // This should throw the exception
logger.information("Child process completed, PID: " +
Poco::NumberFormatter::format(ph.id()) +
", Exit code: " + Poco::NumberFormatter::format(exitCode));
}
catch (const Poco::Exception& ex)
{
logger.error("Exception caught: " + ex.displayText());
// Attempt manual recovery
int status;
if (waitpid(ph.id(), &status, WUNTRACED) > 0)
{
if (WIFEXITED(status))
{
exitCode = WEXITSTATUS(status);
logger.information("Manually acquired exit code: " +
Poco::NumberFormatter::format(exitCode));
}
else
{
logger.warning("Child process terminated abnormally.");
}
}
else
{
logger.error("Failed to manually acquire process exit code.");
}
}
//reaper.join(); // Wait for the reaper thread to finish
}
catch (const Poco::Exception& ex)
{
std::cerr << "Poco exception: " << ex.displayText() << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "Standard exception: " << ex.what() << std::endl;
}
return 0;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Some of the subprocesses from the
fork()
execute very quickly and complete before thewaitpid()
.In this situation poco will throw exception
SystemException("Cannot wait for process")
.But maybe we still want to get it exit code, show as follow:
I don't know how to do it. Is there a better way?
Beta Was this translation helpful? Give feedback.
All reactions