Perl getting segmentation fault while timeout using alarm

折月煮酒 提交于 2019-12-11 18:49:03

问题


I'm writing Perl for the first time and Internet tells me i can use alarm if i have a long running sql query. I need to run a SP which will run for hours in a Perl, and i would like to set a time limit. Below is the last step of my codes but i'm getting segmentation fault when the process runs longer that my timeout, in this occasion sleep(20). Logs shows it finished printing ---1---- and then segmentation fault. How can I fix this?

I tried taking sub DelRef{} out into a seperate .pl and comment the $db part in the third line to test if alarm is working fine, and it worked all right. I'm confused which part went wrong and caused the segmentation fault.

sub DelRef {
    print "starting defRefData\n";
    $db = new Sybapi($user, $password, $server, $margin_database);
    print "entering eval\n";
    my $timeout = 10;
    eval {
            local $SIG{ALRM} = sub { die "timeout\n" };
            print "inside eval now\n";
            alarm($timeout);
            print "---1----\n";
            #sleep(5); --working fine
            sleep(20); #not working, 
            #$db->exec_sql("exec CPN_Margins..clean_up_refData_db '$XrefCode'");
            print "----2----\n";
            alarm(0);
    };
    #alarm(0);
            print "out of eval\n";
    if($@)
    {
        #die unless $@ eq "timeout\n";
        if($@ =~ "timeout\n")
        {
        warn "Timed out!\n";
        #exit 0;
        }
        else{
                print $@;
        }
    }

}

&DelRef();
print "process is done\n";
$db->close();

回答1:


From perldoc (https://perldoc.perl.org/functions/alarm.html)-

It is usually a mistake to intermix alarm and sleep calls, because sleep may be internally implemented on your system with alarm.

If you want to implement timeout, it can be achieved without sleep. Just follow the example mentioned in the link.

EDIT: I have added it here -How to set timeout for a long running Sybase sp in Perl



来源:https://stackoverflow.com/questions/58144739/perl-getting-segmentation-fault-while-timeout-using-alarm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!