Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Fork parent process dies unexpectedly

by Animator (Hermit)
on Nov 19, 2005 at 12:39 UTC ( [id://510079]=note: print w/replies, xml ) Need Help??


in reply to Fork parent process dies unexpectedly

My first questions: you are aware of IO::Select and/or select? (You don't need to fork to be able to use/process more then one client connection.)

There certainly are some issues with this code, but none seem to be critical for the code (as in it's still broken)...

First note: on v5.6.1 the parent does not die. on v5.8.7 (and v5.9.2) the parent does die.

Second: in both programs you are missing: use warnings;. If you are using warnings then you will see one thing in the code: use of uninitalised value in numer gt. Why? Because you have: while (my $kid > 0). Creating a value, not assigning anything to it and then comparing it with 0 is silly. Also note that your while-loop does not assign anything to $kid. (Meaning you should do something more then just removing the my).

Then the client: it's missing use strict; and use warnings;. use warnings is important in this case if you are using it on v5.6.1 (as I initialy was). Why? Because it doesn't work. It seems that <$socket> only works if $socket is a lexical variable (again, on v5.6.1). And that's not the case.

Replies are listed 'Best First'.
Re^2: Fork parent process dies unexpectedly
by Anonymous Monk on Nov 19, 2005 at 13:42 UTC
    Thanks for the reply... I added "use warnings" and found the error in the reaper. I realize that you can use select to handle multiple incoming connections, but ultimately, each connection will spawn a process that can take 1 minute to complete. that is why I opted for "fork". I am curious about the parent still, why does it die? the server code is running on perlV5.8.0. I want the parent to survive forever. How can accomplish this? Thanks, Ian
      (everything in this reply is untested)

      Your main problem is here:

      while (my $conn = $socket->accept) {
      Accept failing is not usually fatal and should not terminate your loop. In particular, if you get a signal, accept can return without result with $! set to EINTR. And you get signals when your children die.

      This doesn't happen in somewhat older perls since they by default have a setting to restart slow systemcalls (but the problem would reappear in really old perls which didn't yet do that).

      In new perls (since 5.8) signals only set a flag and handlers only get executed if the perl dispatcher is in a safe state and sees the flag. But that forced the developers to turn off the restarting of slow systemcalls since otherwise actual running of the signal handler code can be indefinitely delayed (the flag would get set, but since the systemcall gets restarted it still doesn't get to the dispatcher, so nothing gets executed)

      On UNIX versions that support it, you could avoid getting child signals at all by using

      $SIG{CHLD} = "IGNORE";
      But that just sets you up to take the fall when you start handling other kinds of signals.

      So just change the loop to something like:

      use POSIX qw(EINTR); ... while (1) { my $conn = $socket->accept; if (!$conn) { warn("Accept error: $!" if $! != EINTR; next; } .... }
        Thanks to everybody that replied. This seems to be working as I want. I will read more about the EINTR signal. When the last child dies the error rasised by $socket->accept is "No child processes". I will look at Net::Server. It looks like what I need, but I am glad I took this approach to start. Again, my many thanks.

        Ian

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found