mcandre
mcandre

Reputation: 24662

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:

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.

Upvotes: 6

Views: 2393

Answers (1)

socket puppet
socket puppet

Reputation: 3219

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.

Upvotes: 4

Related Questions