4

I'm trying to decide wether it's safe to access a common (read: shared between handler-code and rest of the programm) data structure from a signal handler in perl (v5.14.2) built for x86_64-linux-thread-multi, but target platform is solaris11).

perlipc has the following sample code:

use POSIX ":sys_wait_h"; # for nonblocking read
my %children;
$SIG{CHLD} = sub {
    # don't change $! and $? outside handler
    local ($!, $?);
    my $pid = waitpid(-1, WNOHANG);
    return if $pid == -1;
    return unless defined $children{$pid};
    delete $children{$pid};
    cleanup_child($pid, $?);
};
while (1) {
    my $pid = fork();
    die "cannot fork" unless defined $pid;
    if ($pid == 0) {
        # ...
        exit 0;
    } else {
        $children{$pid}=1;
        # ...
        system($command);
        # ...
   }
}

So, %children is accessed from the while-loop and the handler. This seems to be no problem as:

  1. There won't be two processes having the same pid
  2. Access is keyed by pid (I am not sure if $childer{pid}=1 is atomic and interruptible without causing corruption, though.)

Now, i'm trying to do even more in my handler:

my %categoryForPid;
my %childrenPerCategory;

$SIG{CHLD} = sub {
    # ... acquire pid like above
    my $category = $categoryForPid{$pid};
    $childrenPerCategory{$category}--;
    delete $categoryForPid{$pid};
}

while (1) {
    # ... same as above
    } else {
        $children{$pid}=1;
        my $category = # ... chose some how
        $childrenPerCategory{$category}++;
        $categoryForPid{$pid} = $category;
        # ...
    }
}

The idea here is: every child belongs to a certain category (N to 1). I want to keep track of how many children per category exist. That information could be derived from $categoryForPid, but i think that might be problematic also (e.g., when the subroutine doing the computation gets interrupted while summing up).

So my question is:

  • Do I need to synchronize here somehow?

And on a side note:

  • Are nested invocations of the signal handler possible in perl 5.12, or are they linearized by the interpreter?

Update

In addition to the problem spotted by @goldilocks and his proposed solution I block signals now while updating the data structures to ensure "atomicity":

my $sigset = POSIX::SigSet->new(SIGCHLD);

sub ublk {
    unless (defined sigprocmask(SIG_UNBLOCK, $sigset)) {
        die "Could not unblock SIGCHLD\n";
    }
}

sub blk {
    unless (defined sigprocmask(SIG_BLOCK, $sigset)) {
        die "Could not block SIGCHLD\n";
    }
}

while (1) {
    # ... same as above
    } else {
         blk;
         $children{$pid}=1;
         my $category = # ... chose some how
         $childrenPerCategory{$category}++;
         $categoryForPid{$pid} = $category;
         ublk;
         # ...
    }
}
sschober
  • 2,003
  • 3
  • 24
  • 38

1 Answers1

1

Seems like a bad idea to me. IPC::Semaphore might solve the problem, if you can get them to work properly in a signal handler -- if control does not return until the handler exits, you're out of luck. However, you could get around that by locking in the parent and having the child wait on the lock until initialization is complete; the handler is not involved with the semaphore. You'd only actually need one lock for that, I think. Anyway:

my @array = (1..10);
my $x = 'x';

$SIG{'USR1'} = sub {
    print "SIGUSER1\n";
    undef @array;
    $x = '!!';
};

print "$$\n";

foreach (@array) {
    print "$_:\n";
    sleep(2);
    print "\t$x\n";
    print "\t$array[$_ - 1]\n";
}

Not surprisingly, does this:

2482
1:
    x
    1
2:
    x
    2
3:
SIGUSER1
    !!
Use of uninitialized value within @array in concatenation (.) or string at ./test.pl line 42.

Implying that if you catch the signal at this point:

    my $category = # ... chose some how

$categoryForPid{$pid} will be non-existent in the handler. Etc. Ie, yes you have to synchronize.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67
  • Let me try to understand, what you're saying: 1. Your example shows me that deleting the _entire_ array in the handler has consequences in the main code. Yes, i agree. But i'm only deleting a single entry. 2. I agree also, that the child needs to live at least so long for the main-code to complete the tending of the data structures. Thanks for the hint about the perl cookbook, i now it, but the [freely available version](http://docstore.mik.ua/orelly/perl/cookbook/ch16_18.htm) seems a bit outdated. See [perlipc#safesignals](http://perldoc.perl.org/perlipc.html#Deferred-Signals-(Safe-Signals)) – sschober May 15 '12 at 12:13
  • @sschober *"only deleting one entry"* is like "I'm not rolling *as many* dice". The issue in your code is to do with what might not exist when the handler is called. WRT the outdatedness of the Cookbook, true, but it does mean you have to find evidence that things have changed substantially in later versions of perl 5, because certainly `malloc` and `printf` (C library calls) have not. OTOH, those two in particular shouldn't be hard to avoid. I'm not so sure that semaphores will work in a signal handler, BTW, I'll add an edit about that. – CodeClown42 May 15 '12 at 12:20
  • do you think blocking `SIGCHLD` for the duration of the critical section might be an option? – sschober May 15 '12 at 12:28
  • @sschober Won't that screw up the logic? Another possibility with the semaphores which avoids using them in the handler would be for the parent to lock, fork the child, the first thing the child does is wait on the lock, the parent completes the initialization and unlocks. This way the child cannot terminate early. I think you can actually get away with a single sem that way, as long as the child releases right away too. – CodeClown42 May 15 '12 at 12:44
  • No, I don't think blocking the signals would screw up the semantics, as there is only a single thread involved here. Using semaphores would produce a dead lock, i guess. The lock would be acquired in the main loop, the handler would try to acquire the lock again and then we'd hang forever. Blocking the signal OTOH is not problematic as blocked signals seem to be queued up and delivered later. Multiple occurrences of the same signal seem to be delivered as just one occurrence. Which is no problem for me, as I detect those situation using `waitpid(-1,...)`. – sschober May 15 '12 at 13:05
  • Note: the signal delivery behavior stated above is only based on assumption and [empirical evidence](https://gist.github.com/2701627) – sschober May 15 '12 at 13:10
  • @sschober If you are confident the blocked signal is just deferred, then that's the simplest solution. But I think you misunderstood my (2nd) idea about the semaphore, the one *not* involving the handler. There is no possibility of deadlock there: the parent holds it before the child starts, the child is waiting when the parent releases it, then releases it immediately when it gets it. – CodeClown42 May 15 '12 at 13:16
  • BTW signal handling has been fixed WRT re-entrant problems: http://perldoc.perl.org/perlipc.html#Deferred-Signals-%28Safe-Signals%29 -- they are now "safe". – CodeClown42 May 15 '12 at 13:19
  • ah, i see. i misunderstood you for sure :) – sschober May 15 '12 at 13:24