为什么我的线程Perl脚本会出现段错误?

时间:2011-02-18 06:31:21

标签: linux multithreading perl segmentation-fault

我正在编写一个curses脚本,该脚本在处理SIGINT后需要清理才能使终端返回其原始状态。

启用信号处理程序时会出现段错误。

为了支持,我删除了所有的curses代码以解决问题。

代码:

#!/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"; }

示例错误跟踪(90%的时间段为segfaults):

$ ./threadtest.perl

...

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

$

规格:

  • threads 1.72
  • archname“”
  • os“”
  • Perl 5.10.1(Debian附带)Debian
  • 6挤压

初步印象:

我认为当自定义信号处理程序获取控制权时会出现问题。这会以某种方式阻止创建下一个线程,从而导致段错误。

Perl的默认SIGINT处理程序是否运行特殊代码来安全地结束线程创建的评估?如果是这样,我想解决方案是将copypasta引入自定义处理程序。

1 个答案:

答案 0 :(得分:4)

revision history for the threads module包含:

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

表明在1.73之前的版本中,在创建和销毁线程期间存在已知的信号处理问题。升级您的threads模块。