Running multiple ChildProcesses, my bug or Juce bug?


#1

I am using the following class to launch and wait for multiple ChildProcesses. (Also as a special case this is also used to run single processes so that the code using this library class doesn't need to itself do anything differently if there is just one child process or several of them to run.) It might be a bit dirty and dumb but it appears to work ok on Windows. It does not however appear to work as expected on OS-X when a child process exists with an error. (ie an exit code other than 0.) The behavior appears to be that the code path that checks for the exit code 0 is executed, so the code using the class ends up believing everything went OK, when it didn't. Am I doing something wrong in this or is the Juce ChildProcess returning the exit code 0 when it shouldn't or something like that? (I have confirmed in the Terminal that the executables I am running return 0 when they succeed and some other number (for example 255) when they exit with an erro.)


class child_processes
{
public:
using ChildProcessPtr=std::unique_ptr<ChildProcess>;
child_processes() {}
void set_spin_sleep_amount(int ms) { m_spin_sleep=ms; }
void add_and_start_task(StringArray arguments)
{
m_processes.push_back(jcdp::make_unique<ChildProcess>());
m_processes.back()->start(arguments);
}
void add_task(StringArray arguments)
{
m_processes.push_back(jcdp::make_unique<ChildProcess>());
}

// All must succeed, otherwise returns string with the output of some failed process
String wait_for_finished(int wait_ms)
{
int success_count=0;
int count=m_processes.size();
double t0=Time::getMillisecondCounterHiRes();
while (1)
{
for (int i=0;i<m_processes.size();++i)
{
if (m_processes[i]->isRunning()==false)
{
if (m_processes[i]->getExitCode()==0)
++success_count;
else
return m_processes[i]->readAllProcessOutput();
m_processes.erase(m_processes.begin()+i);
}
}
if (success_count==count)
return String();
double t1=Time::getMillisecondCounterHiRes();
if (t1-t0>wait_ms)
return "Wait time exceeded";
Thread::sleep(m_spin_sleep);
}
return "Unknown error";
}
private:
std::vector<ChildProcessPtr> m_processes;
int m_spin_sleep=10;
};

#2

I was able to work around the problem by searching the process output text for words like "error" or "doesn't work", which is not ideal but will have to do for now...

I will try to repro the problem with a simple test program some day soon, to see if Juce incorrectly gives 0 as the process return code on OS-X when it should not.