简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM