Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

SIG CHLD IGNORE and wait at same time

by vsespb (Chaplain)
on Aug 03, 2013 at 10:12 UTC ( [id://1047688]=perlquestion: print w/replies, xml ) Need Help??

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

use strict; use warnings; use POSIX; if (my $pid = fork) { sleep 1; alarm 10; kill (POSIX::SIGUSR2, $pid); $SIG{CHLD} = 'IGNORE'; while( wait() != -1 ){ print STDERR "wait\n"}; } else { $SIG{USR2} = sub { print "CHILD $$ EXIT\n"; exit(1); }; sleep 1 while (1); } print STDERR "DONE\n";

This code dies with ALARM, only on OpenBSD, only on Perl 5.12.x. (installed with perlbrew)

And it usually works well with openbsd vendor perl 5.12.2:

Locally applied patches: CVE-2010-0405 Updated CGI to 3.51 Updated Test::Simple to 0.98 Updated List::Util to 1.23 CVE-2011-1487 Updated Digest to 1.17 CVE-2011-2939 uncommitted-changes

Note that $SIG{CHLD} = 'IGNORE'; is a special perl feature to auto-reap zombies (i.e. it acts as calling wait() in signal handler), however replacing $SIG{CHLD} = 'IGNORE' with:

$SIG{CHLD} = sub { while( wait() != -1 ){ print STDERR "wait0\n"}; };
fixes problem

I am wondering is this a bug or feature and mixing wait() in signal handlers and wait() in main code is bad idea?

Replies are listed 'Best First'.
Re: SIG CHLD IGNORE and wait at same time
by rjt (Curate) on Aug 03, 2013 at 11:08 UTC

    You have a race condition in your parent code:

    kill (POSIX::SIGUSR2, $pid); $SIG{CHLD} = 'IGNORE';

    (You send the signal to your child before you set the $SIG{CHLD} handler.)

    Try swapping those two statements, for a start.

    Beyond that, I wonder why you set $SIG{CHLD} = 'IGNORE' but then loop on wait() in the normal execution flow? It's unlikely that's causing your bug, but the docs are a little non-committal on this (emphasis mine):

    Calling wait() with $SIG{CHLD} set to "IGNORE" usually returns -1 on such platforms.

    'IGNORE' is most useful when you really don't care. But if you don't want to exit until all of your children exit, I'd recommend just using your own wait() or waitpid() loop, perhaps in conjunction with a custom signal handler if you need out-of-band child exit notifications.

      Try swapping those two statements, for a start.
      No, it did not help.
      You have a race condition in your parent code:
      I don't think it was race condition in this case, because this code should work with and without SIG CHLD IGNORE statement.
      IGNORE' is most useful when you really don't care.
      Well, for example I want to reap zombies while program run, but at the same time I want to wait all child processes at exit.
      I'd recommend just using your own wait() or waitpid() loop, perhaps in conjunction with a custom signal handler if you need out-of-band child exit notifications.
      Yes, as I told, this fixes problem:
      $SIG{CHLD} = sub { while( wait() != -1 ){ print STDERR "wait0\n"}; };
      so probably there is a workaround.
      usually returns -1
      Yes, indeed. Ok, thanks.
        I don't think it was race condition in this case, because this code should work with and without SIG CHLD IGNORE statement.

        Fair enough; your further testing speaks to that. I'd still recommend setting up the handler before the kill, as a matter of good practice if nothing else.

        Well, for example I want to reap zombies while program run, but at the same time I want to wait all child processes at exit.

        OK. Luckily, that's quite easy to do, with only one wait in your code. See perlipc for more info, but here's a handler inspired by that:

        use POSIX ':sys_wait_h'; # WNOHANG my %pids; # Parent's hash of child pids $SIG{CHLD} = sub { local ($?, $!); # Don't change $! or $? outside handler until (-1 == (my $pid = waitpid(-1, WNOHANG))) { return if $pid == 0; # Processes still running next unless delete $pids{$pid}; say "$pid exited with status $?"; } };

        In your parent, just set $pids{$pid} = 1 every time you fork, and the SIGCHLD handler will clean them up. In your parent's main loop, you then know not to exit until %pids is empty. You need to track your pids somehow, if you intend to send signals to them, so you may as well take advantage of that.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2024-07-19 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.