Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

browser cancels request-> forked procs die?

by jorg (Friar)
on Nov 26, 2003 at 15:53 UTC ( #310291=perlquestion: print w/replies, xml ) Need Help??

jorg has asked for the wisdom of the Perl Monks concerning the following question:


Say i fork a few processes in a CGI environment. When the user sitting in front of his browser presses "cancel" during processing what happens to these forked processes?
Do they continue running until they are finished, or are they killed by apache? Should i install some signalhandlers for this to catch this event? I guess things can get quite tricky here..


"Do or do not, there is no try" -- Yoda
  • Comment on browser cancels request-> forked procs die?

Replies are listed 'Best First'.
Re: browser cancels request-> forked procs die?
by etcshadow (Priest) on Nov 26, 2003 at 16:35 UTC
    Here's a link to some info for you, make what you will of it:

    Combine that with, for example IPC::Run, and a pumping subprocess, so that you can kill the subprocess if you detect the socket close.

    I would, however, caution you, in general, on going down this path, because proxy servers will break this. You may not have a proxy server in place now, but you may want to add one later. Also, the client may be behind a proxy, themselves, and you've got no control over that.

    Not an editor command: Wq
      owkay, this might be a possibility then because i control the the users of this app more or less. I haven't used IPC::Run yet so i don't know what you mean by a pumping subprocess, i'll have a look into this tnx.


      "Do or do not, there is no try" -- Yoda
Re: browser cancels request-> forked procs die?
by mpeppler (Vicar) on Nov 26, 2003 at 16:15 UTC
    It's been a while since I did this sort of thing, but IIRC you can't reliably catch the fact that the user hit STOP on the browser, so it is most likely that your forked progs will continue running until they complete.


      ok point taken. So what can I do to prevent that these processes will run forever? There is an odd case that the programs i'm calling go into an endless loop. Can i do a timeout of some sort, and then kill the process off?


      "Do or do not, there is no try" -- Yoda
        It's a little hard to say off-hand as I don't know what your subprocess does. If you have control over the subprocesses source then you could put an alarm in its code and terminate it if it runs for more than X seconds. Or you could do the same thing in the parent (your CGI/modperl handler) where you could do something like (pseudo code):
        $SIG{ALRM} = sub { die 'Timeout'; }; alarm( some decent duration ); eval { fork the child here wait for the child (waitpid() or similar) if the child returns reset the alarm (alarm(0)) }; if($@) { if($@ =~ /Timeout/) { # child exceeded authorized run time, so kill it kill(15, $child); } else { handle other fatal problems.... } }
        It's up to you to determine what amount of time your child process should normally run, so that you don't interrupt a normally executing process.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://310291]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2021-09-28 04:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found