Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

browser cancels request-> forked procs die?

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

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

Hi,

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..



Jorg

"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: http://perl.apache.org/docs/1.0/guide/debug.html#Handling_the__User_pressed_Stop_button__case

    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.


    ------------
    :Wq
    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.


      Jorg

      "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.

    Michael

      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?


      Jorg

      "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.

        Michael

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found