1

我正在运行一个抓取工具,它从数百个网页中获取一些关键信息。一切正常,但在超时时我遇到了分段错误。

use Perl::Unsafe::Signals;
require LWPx::ParanoidAgent;
...
$ua = LWPx::ParanoidAgent->new();
$ua->timeout(60);
...
local $SIG{ALRM} = sub {
    print "Timeout occurred.  Skipping to next record.\n";
};
alarm 60; # give each journal a minute to respond, in total.
UNSAFE_SIGNALS {
    ...
    # some calls like the following: 
    my $pageResponse = $ua->get($url);
    if ($pageResponse->is_success) {
        # calls to a sub
        # that also does $ua->get()
        # I think it fails inside the sub (if that makes a diff)
    }
};
alarm 0; # clear the timeout.

运行:为 i686-linux 构建的 perl 5,版本 16,subversion 3 (v5.16.3)

该脚本在超时后引发分段错误。我得到“超时发生”的打印,然后是分段错误。

有人对可能发生的事情有任何线索吗?调试建议?

额外信息:我只有一个“eval”块,而不是之前的 UNSAFE_SIGNALS 块,它只会在发生超时时挂起。

4

1 回答 1

1

该问题通过仅将 eval 块放在 $ua->get 请求周围来解决,仅此而已。问题似乎在于根据触发警报的时间而达到不一致的状态。

对我的所有获取请求使用以下代码:

sub uaGetWrapper($) {
    my $url = shift;
    my $response;
    eval {
        local $SIG{ALRM} = sub { die "timeout"; };
        alarm 60;
        $response = $ua->get($url);
        alarm 0;
    };

    if ($@ && $@ =~ /timeout/) {
        return 0; # return false on a timeout.
    }

    return $response;
}
于 2013-04-20T04:50:28.780 回答