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;

exit if fork() > 0; # fork once again

close STDIN;
open STDOUT, '+>>', '/path/to/apache/error_log'; # any way to get path to the 
current error_log and pass it in from the caller as an argument or as an 
opened filehandle ?
open STDERR, '>&STDOUT';

exec ( @ARGV ) or die "Failed to exec subprocess: [EMAIL PROTECTED]";

===end===

Here's an updated helper subroutine:

sub safe_exec {
    my $in = $apr->spawn_proc_prog ( '/path/to/exec_helper.pl', [EMAIL 
PROTECTED] );
    
    # makes us sleep until the helper is forked into background and it's 
handlers are untied from us
    eval { read $in, my($buffer), 1024 };
    if ( $@ ) {
        print STDERR "exec [EMAIL PROTECTED]";
    }
    close $in;
}

I had to use this form of spawn_proc_prog to get something to wait on.

-- 
Best Regards,
Igor Shevchenko

Reply via email to