in reply to Re: Attempt to free unreferenced scalar...
in thread Attempt to free unreferenced scalar...

> I commented $SIG{CHLD} and the problem really went away!!!
Good, that's what I thought it was all along.

>it has to be still multithreaded. Is this possible with waitpid?

Oh, yes, that's why I suggested it. The key to the multithreadedness of your program is the spawn function. You aren't even going to touch the spawn function.

The example of what you need to do is in the manual. If you do waitpid(-1, &WNOHANG), then it checks to see if there is a zombie, and if there is one, it cleans it up. Unlike wait, it returns immediately whether there is a zombie or not. If you use:

do { $kid = waitpid(-1,&WNOHANG); } until $kid == -1;

then your program will clean up all the outstanding zombies. If there are no zombies, waitpid will return -1 immediately and the loop will execute only once.

You can stick this code into your program at the top of the main loop, and every time your server accepts a new client, it will try to clean up any outstanding zombies. Get rid of $waitedpid and the signal handler. Leave spawn the way it is. Put the waitpid loop in just before the call to spawn. Don't forget to use POSIX ":sys_wait_h" like it says in perlfunc.

Hope this helps. Send me email if you can't get it working.

Replies are listed 'Best First'.
Re: Attempt to free unreferenced scalar...
by Daniellek (Sexton) on Dec 07, 2000 at 20:02 UTC
    OK, I did it as you said and... it works!
    No more errors, no more unhappy people ;)

    Thanks! If you ever be in Krakow (Poland), I'll buy you a beer :)

    -- Daniellek
      I'm glad I could be helpful! If I'm ever in Krakow, I'll let you buy me a beer.

        I hope You're still tracking this thread, because i've got real big problem with this program after "patching" it...

        Below is the changed fragment (old code is commented), and the problem is that it seems it's not multithreaded! I watched `ps fauxw` and during heavy load, while old version was runnig there were from 3 to 8 processes in beautiful tree, now it seems it has 1 child only at a time... Related problem is that during heavy load like this i get "connetcion refused" on clients...

        Is there something wrong in this code? Maybe i just misunderstood Your "patching instructions"

        Please Help!

        my $dbh = DBI->connect("DBI:mysql:database=qmail;host=localhost", "qmail", $pass, {'RaiseError' => 1}); logmsg "server started on port $port"; my $waitedpid = 0; my $paddr; sub REAPER { #!!!!!!!!!!!!v $waitedpid = wait; $SIG{CHLD} = \&REAPER; # loathe sysV #logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER; #!!!!!!!!!!!!!!^ for ( $waitedpid = 0; ($paddr = accept(Client,Server)) || $waitedpid; $waitedpid = 0, close Client) { next if $waitedpid and not $paddr; my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; #do { # $kid = waitpid(-1,&WNOHANG); # } until $kid == -1; spawn sub { my $line = <STDIN>; chomp $line; my @param = split (/ /,$line);
        -- Daniellek