A program of mine that uses Poco to launch processes was mysteriously hanging, apparantly randomly. At long last I have been able to reproduce the problem. The code is shown below.
The problem happens if built with or without -g but does not happen when running under the debugger.
I am running on Linux using GCC 4.4.5, using Poco version 1.4.1.
Basically, I create a thread does a launch followed by a wait. In the meantime the main thread kills the pid. This causes the thread to return from the wait but the main thread then hangs on the exit.
boost::thread* the_thread = new boost::thread(&my_thread::run, this);
Poco::ProcessHandle process_handle = Poco::Process::launch("./beta", no_args);
pid = process_handle.id();
int exit_status = process_handle.wait();
std::cout << "run_beta has finished" << std::endl;
int main(int argc, char* argv)
std::cout << "kill complete will now exit." << std::endl;
This is extremely cut down just to show the problem. My actual app communicates the pid via a concurrent queue object and it cleans up the boost thread object properly, doing the required join. The app is valgrind-clean (i.e no corruption, memory leaks etc). I realise the above app leaks a boost thread and does not synchronise access to the pid properly; that's why the sleep is there. It's a crude kludge to make the app simpler. The "./beta" is it launching a shellscript that goes to sleep for a long time.
An interesting point is that when I replace ./beta with sleep (adjusting the arg vector to specify a large sleep value) the problem mysteriously goes away. So maybe it is somehow connected to launching processes that are shellscripts.