Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:26:22+00:00 2026-06-05T13:26:22+00:00

I have a function which calls several other functions, e.g. sub do_work { send_mail();

  • 0

I have a function which calls several other functions, e.g.

sub do_work {
    send_mail();
    send_soap_envelope();
    send_rpc();
}

The called functions may possibly hang, so I want to stop them after some timeout. I want to avoid forking because it is expensive in my context (e.g. database handles need to be recreated after each fork). I’ve come up with the following scheme:

sub timeout {
    my ($code) = @_;
    eval {
        alarm 2;
        local $SIG{ALRM} = sub { die 'timeout' };
        &$code;
        alarm 0;
    };
    # handling of $@ eq 'timeout' removed for brevity
}

sub do_work {
    timeout \&send_mail;
    timeout \&send_soap_envelope;
    timeout \&send_rpc;
};

The timeout() function (in this example hardcoded to a timeout of 2 seconds) uses an eval block as a means of aborting the execution of the payload function using die.

This works fine in my test scenarios, but I’m feeling uneasy about what will happen if the die interrupts the payload function while the Perl interpreter is not in a “safe state”, e.g. while it is processing an XS subroutine. Is my gut feeling right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:26:25+00:00Added an answer on June 5, 2026 at 1:26 pm

    Since 5.8.1, Perl uses “safe signal handling”. It doesn’t give your signal handler to the system, it gives a safe signal handler instead. This safe signal handler simply notes that a signal was received and returns. Between executing Perl opcodes, the interpreter checks if any signals were received and calls your signal handler if there was.

    That means that signals will not get processed in the middle of a long op, such as a long XS call or a long regex match. Signals interrupt most system calls, so your signal handler will be called shortly after the signal comes in ever if you are in the middle of a blocking system call (e.g. sleep, read, etc)

    alarm(2);
    my $s = time;
    $SIG{ALRM} = sub {
       my $e = time;
       print $e-$s, "\n";  # 6, not 2.
    };
    ('a' x 25) =~ /a*a*a*a*a*a*a*a*a*(?:b|c)/;
    

    * — In order to speed up programs, it’s checked a little less often now, but you shouldn’t notice the difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several functions which use jquery that are called across different pages. I'm
Here's the setup. I have a C++ program which calls several functions, all of
I have a C program which calls several functions from different files, and in
I have foreach function which calls specified function on every element which it contains.
i have written a c++/cli program which calls my native c++ function which has
I have a simple onclick feature which calls a function but im getting an
I have a class ( EAGLView ) which calls a member function of a
I have a python program which calls a C function. I send some structures
I have a helper function, which basically calls CompareTo on two objects, but does
I have an onclick function which performs several tasks. In another JavaScript function I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.