THRIFT-4065 fix SIGCHLD handling for perl ForkingServer and document restrictions
Client: perl

This closes #1176
diff --git a/lib/perl/README.md b/lib/perl/README.md
index e6451ec..0540948 100644
--- a/lib/perl/README.md
+++ b/lib/perl/README.md
@@ -47,6 +47,14 @@
 
 Please see tutoral and test dirs for examples.
 
+The Perl ForkingServer ignores SIGCHLD allowing the forks to be
+reaped by the operating system naturally when they exit.  This means
+one cannot use a custom SIGCHLD handler in the consuming perl
+implementation that calls serve().  It is acceptable to use
+a custom SIGCHLD handler within a thrift handler implementation
+as the ForkingServer resets the forked child process to use
+default signal handling.
+
 Dependencies
 ============
 
diff --git a/lib/perl/lib/Thrift/Server.pm b/lib/perl/lib/Thrift/Server.pm
index ac1e17d..5829e67 100644
--- a/lib/perl/lib/Thrift/Server.pm
+++ b/lib/perl/lib/Thrift/Server.pm
@@ -235,10 +235,6 @@
     my $itrans = shift;
     my $otrans = shift;
 
-    # add before collect, otherwise you race w/ waitpid
-    $self->{children}->{$pid} = 1;
-    $self->_collectChildren();
-
     # Parent must close socket or the connection may not get closed promptly
     $self->tryClose($itrans);
     $self->tryClose($otrans);
@@ -254,6 +250,8 @@
 
     my $ecode = 0;
     eval {
+        # THRIFT-4065 ensure child process has normal signal handling in case thrift handler uses it
+        $SIG{CHLD} = 'DEFAULT';
         while (1)
         {
             $self->{processor}->process($iprot, $oprot);
@@ -292,24 +290,4 @@
     }
 }
 
-sub _collectChildren
-{
-    my $self = shift;
-
-    while (scalar keys %{$self->{children}})
-    {
-        my $pid    = waitpid(-1, WNOHANG);
-
-        if ($pid>0)
-        {
-            delete $self->{children}->{$pid};
-        }
-        else
-        {
-            last;
-        }
-    }
-}
-
-
 1;