exit parent if child dies unexpectedly (instead of fork-bombing)
Jonathan Rockway [Tue, 12 May 2009 01:00:12 +0000 (20:00 -0500)]
lib/MooseX/Runnable/Invocation/Plugin/Restart/Base.pm

index 9d516db..1802a0c 100644 (file)
@@ -44,6 +44,14 @@ around 'run' => sub {
             # handle the case where the child dies unexpectedly
             waitpid $self->child_pid, 0;
             $self->clear_child_pid;
+            my ($code, $sig) = ($? >> 8, $? & 127);
+            eval { $self->_debug_message(
+                "Exiting early, child died with status $code (signal $sig).",
+            )};
+
+            # relay the error up, so the shell (etc.) can see it
+            kill $sig, $$ if $sig; # no-op?
+            exit $code;
         };
 
         # parent
@@ -71,12 +79,20 @@ around 'run' => sub {
                         kill 'KILL', $pid2;
                     };
                     waitpid $pid2, 0;
-                    $child_body->();
+                    my $code = $? >> 8;
+                    if($code == 0){
+                        goto $child_body;
+                    }
+                    else {
+                        eval { $self->_debug_message(
+                            "Child exited with non-zero status; aborting.",
+                        )};
+                        exit $code;
+                    }
                 }
                 else {
                     # child? actually do the work
                     exit $self->$next(@args);
-
                 }
             }
         };