--- apt-cacher2 2010-04-26 12:43:57.000000000 +1200 +++ /home/craig/apt-cacher-1.6.10ubuntu3~/apt-cacher2 2012-01-04 10:35:59.747005709 +1300 @@ -59,6 +59,7 @@ use HTTP::Response; use HTTP::Date; use Sys::Hostname; +use POSIX ":sys_wait_h"; # Include the library for the config file parser require 'apt-cacher-lib.pl'; @@ -89,7 +90,7 @@ my @cache_control; my $listeners; -my @childPids; +my %childPids; my $terminating; # Function prototypes @@ -237,9 +238,11 @@ for ($listeners->handles) {$_->shutdown(2)}; } - for(@childPids) { - &debug_message("killing subprocess: $_"); - kill 15, $_; + for(keys(%childPids)) { + if($childPids{$_} == 1) { + &debug_message("killing subprocess: $_"); + kill 15, $_; + } }; exit(0); } @@ -768,7 +771,7 @@ } if ($pid == 0) { # child, the fetcher thread - undef @childPids; + undef %childPids; $0="$0 [$host$uri]"; # Visible to ps and friends close($fromfile); @@ -780,7 +783,7 @@ exit(0); } # parent continues - push @childPids, $pid; + $childPids{$pid} = 1; debug_message("registered child process: $pid"); close($pkfd); } @@ -1169,7 +1172,7 @@ if ($lc_pid == 0) { # Child, the libcurl thread debug_message('Init libcurl thread'); - undef @childPids; + undef %childPids; $con->close; $source->close; $aclog_fh->close; @@ -1787,7 +1798,14 @@ $| = 1; #Signal Handlers -$SIG{CHLD} = 'IGNORE'; +#$SIG{CHLD} = 'IGNORE'; +$SIG{CHLD} = sub { + my $child; + while(($child = waitpid(-1, WNOHANG)) > 0) { + debug_message("Child $child just finished; removing from childPids list"); + $childPids{$child} = 0; #Don't delete; that seems to tickle some perl race conditions. + } +}; $SIG{TERM} = sub {debug_message('received SIGTERM, terminating'); exit}; $SIG{HUP} = \&reload_config; $SIG{USR1} = \&toggle_debug; @@ -1893,11 +1911,11 @@ if ($pid > 0) { # parent debug_message("registered child process: $pid"); - push @childPids, $pid; + $childPids{$pid} = 1; next; } # child - undef @childPids; + undef %childPids; undef $listeners; &handle_connection($newcon);