Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my threaded Perl script segfault?

I'm writing a curses script which requires cleanup after processing SIGINT in order to return the terminal back to its original status.

I get a segfault when the signal handler is enabled.

For support's sake, I removed all the curses code to boil the problem down.

Code:

#!/usr/bin/env perl

use strict;
use warnings;
use threads;

sub cleanup { exit 0; }

sub run { while (1) {} }

# comment this line and the problem disappears
$SIG{INT} = \&cleanup;

foreach my $i (1..100) {
    print "Creating this thread\n";

    my $t = threads->create(\&run);

    print "Created that thread\n";
}

while (1) { print "Looping\n"; }

Sample Error Trace (segfaults 90% of the time):

$ ./threadtest.perl

...

Creating this thread
Creating that thread
Detaching this thread
Detaching that thread
Creating this thread
^CSegmentation fault

$

Specs:

  • threads 1.72
  • archname ""
  • os ""
  • Perl 5.10.1 (came with Debian) Debian
  • 6 Squeeze

Initial Impression:

I think the problem occurs when the custom signal handler grabs control. This somehow prevents the next thread from being created, resulting in a segfault.

Does Perl's default SIGINT handler run special code to safely end evaluation of thread creation? If so, I imagine the solution is to copypasta into the custom handler.

like image 700
mcandre Avatar asked Feb 18 '11 06:02

mcandre


1 Answers

The revision history for the threads module contains:

1.73 Mon Jun  8 13:17:04 2009
- Signal handling during thread creation/destruction (John Wright)
- Upgraded ppport.h to Devel::PPPort 3.17

which suggests that there was a known problem with signal handling during thread creation and destruction in versions earlier than 1.73. Upgrade your threads module.

like image 146
socket puppet Avatar answered Oct 17 '22 19:10

socket puppet