title: Threaded perl and signal handlers
body: <p>I am using the <a href="http://search.cpan.org/~elizabeth/Thread-Pool-0.33/lib/Thread/Pool.pm" rel="nofollow"><code>Thread::Pool</code></a> module in perl to parallelize some perl code.  This process takes a while and occasionally I will kill it from the command line with a <a href="http://perldoc.perl.org/perlipc.html#Signals" rel="nofollow"><code>SIGINT</code></a>.  Doing so causes the program to end abruptly, as I expected. This leaves some messy temporary files around, so I'd like to install a signal handler.  I did this:</p>
  
  <pre><code>sub INT_Handler{
      #clean up code
      exit(1);
  }
  $SIG{'INT'} = 'INT_handler';
  </code></pre>
  
  <p>before creating the thread pool and starting the threads.  Now when I send the <a href="http://perldoc.perl.org/perlipc.html#Signals" rel="nofollow"><code>SIGINT</code></a>, the <a href="http://perldoc.perl.org/perlthrtut.html#Boss/Worker" rel="nofollow">worker threads</a> that are running die, but the pool promptly launches another set workers to handle the next set of jobs and continues running.  Why doesn't the call to exit in the signal handler exit the main thread?  What do I need to stop the process from running?</p>
  
  <p><strong>Edited in response to mob's comment</strong></p>
  
  <p>** Further edit **</p>
  
  <p>Here is an example I wrote up.</p>
  
  <pre><code>use Thread::Pool;
  
  sub INT_handler{
      print "Handler\n";
      exit(1);
  }
  
  $SIG{'INT'}='INT_handler';
  
  sub f{
      print "Started a thread " . rand(10000) . "\n";
      sleep(10);
  }
  
  my $pool;
  my $submit = \&amp;f;
  
  if (0){
   $pool = Thread::Pool-&gt;new({do=&gt;'f', workers=&gt;5});
    $submit = sub{ $pool-&gt;job; }
  }
  
  for (my $i = 0; $i &lt; 100; $i++){ $submit-&gt;(); }
  
  $pool-&gt;shutdown if defined $pool;
  </code></pre>
  
  <p>with <code>0</code>, I see the expected result</p>
  
  <pre><code>h:57 Sep 15 16:15:19&gt; perl tp.pl
  Started a thread 3224.83224635111
  Handler
  </code></pre>
  
  <p>but with <code>1</code>, this happens</p>
  
  <pre><code>h:57 Sep 15 16:14:56&gt; perl tp.pl
  Started a thread 5034.63673711853
  Started a thread 9300.99967009486
  Started a thread 1394.45532885478
  Started a thread 3356.0428193687
  Started a thread 1424.4741558014
  </code></pre>
  
  <p>etc and the handler doesn't get entered and the process continues running.  I had to kill the process with a signal other than <a href="http://perldoc.perl.org/perlipc.html#Signals" rel="nofollow"><code>SIGINT</code></a>.  Without the handler, both cases simply exit when passed a <a href="http://perldoc.perl.org/perlipc.html#Signals" rel="nofollow"><code>SIGINT</code></a>.</p>