Igor Shevchenko wrote:
On Saturday 12 March 2005 02:52, you wrote:

Igor Shevchenko wrote:

Hi all,

Some time ago we had a thread about starting long-running background
processes from mp2. I've found a race condition in our last solution. The
helper background script could be killed by apache cleanup handler before
it had a chance to fork into background and do "setsid". Here's the
script itself:

===start===
#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX ();

chdir '/';

POSIX::setsid;

You mean you have moved the setsid call to an earlier phase? so setsid is called before spawn_proc_prog() has returned?


This is the first part.
The second is that STDOUT is closed (by re-opening it) only after fork(), and the caller sub (exec_helper) will exit only after subprocess's STDOUT is closed. I think this should close this race condition as the caller subroutine will exit only after the background subprocess is running and is fully detached.

In which case it's probably the best to use waitpid instead and which will block the return of the subprocess
http://perl.apache.org/docs/1.0/guide/performance.html#Avoiding_Zombie_Processes
Unfortunately this is probably unix-specific. I wonder how can we make a crossplatform API that will do just that, fork a long running process for you, so you won't need to write any wrapper hacks.


--
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Reply via email to