develooper Front page | perl.perl5.porters | Postings from June 2008

[RFC] common test code for timed bail out

Thread Next
Jerry D. Hedden
June 26, 2008 08:31
[RFC] common test code for timed bail out
Message ID:
The attached patch contains my proposal for addressing the
following Perl TODO:

=head2 common test code for timed bail out

Write portable self destruct code for tests to stop them burning CPU in
infinite loops. This needs to avoid using alarm, as some of the tests are
testing alarm/sleep or timers.

Here's the code to be added to

# Forks a child process that kills the parent if the timeout is reached
sub timeout ($)
    my $timeout = shift;

    my $child_pid;
    eval { $child_pid = fork() };
    return if (! defined($child_pid));   # Fork failed

    # Child process
    if ($child_pid == 0) {
        my $ppid = getppid();
        exit(0) if (! $ppid);   # Failed getting parent PID

        sleep($timeout - 2) if ($timeout > 2);   # Workaround for perlbug #49073

        # Kill parent if it still exists
        if (kill(0, $ppid)) {
            _diag("Test process (PID=$$) timed out");
            kill('KILL', $ppid);

    # Add END block to parent to clean up child process
    eval "END { kill('KILL', $child_pid) if (kill(0, $child_pid)); }";

Also, please comment on which core test files should be
patched to use this (if and when it is incorporated).

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About