an update to exec_helper.pl script

[prev] [thread] [next] [Date index for 2005/03/12]

From: Igor Shevchenko
Subject: an update to exec_helper.pl script
Date: 00:17 on 12 Mar 2005
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: $@\n";

===end===

Here's an updated helper subroutine:

sub safe_exec {
    my $in = $apr->spawn_proc_prog ( '/path/to/exec_helper.pl', \@_ );
    
    # 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 error=$@\n";
    }
    close $in;
}

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

        -- 
        Best Regards,
Igor Shevchenko

(message missing)

an update to exec_helper.pl script
Igor Shevchenko 23:29 on 10 Mar 2005

an update to exec_helper.pl script
Igor Shevchenko 00:17 on 12 Mar 2005

Re: an update to exec_helper.pl script
Stas Bekman 00:52 on 12 Mar 2005

Re: an update to exec_helper.pl script
Igor Shevchenko 01:18 on 12 Mar 2005

Re: an update to exec_helper.pl script
Stas Bekman 01:29 on 12 Mar 2005

Generated at 08:39 on 15 Mar 2005 by mariachi v0.52