Skip to content

Commit 4712a4c

Browse files
committed
t/test.pl: Add ability to cancel an watchdog timer
A watchdog affects the entire rest of the file. As more tests get added to a test file, they could end up triggering the watchdog. This commit adds the capability to cancel a watchdog if the potentially problematic test finishes in time.
1 parent 505ba11 commit 4712a4c

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

t/test.pl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,8 +1733,34 @@ sub warning_like {
17331733
# _AFTER_ the 'threads' module is loaded.
17341734
sub watchdog ($;$)
17351735
{
1736+
CORE::state $watchdog;
1737+
CORE::state $watchdog_thread;
17361738
my $timeout = shift;
1737-
my $method = shift || "";
1739+
1740+
# If cancelling, use the state variables to know which method was used to
1741+
# create the watchdog.
1742+
if ($timeout == 0) {
1743+
if ($watchdog_thread) {
1744+
$watchdog_thread->kill('KILL');
1745+
undef $watch_dog_thread;
1746+
}
1747+
elsif ($watchdog) {
1748+
kill('KILL', $watchdog);
1749+
undef $watch_dog;
1750+
}
1751+
else {
1752+
alarm(0);
1753+
}
1754+
1755+
return;
1756+
}
1757+
1758+
# Make sure these aren't defined.
1759+
undef $watchdog;
1760+
undef $watchdog_thread;
1761+
1762+
my $method = shift || "";
1763+
17381764
my $timeout_msg = 'Test process timed out - terminating';
17391765

17401766
# Accept either spelling
@@ -1763,7 +1789,9 @@ ($;$)
17631789
if (!$threads_on || $method eq "process") {
17641790

17651791
# On Windows and VMS, try launching a watchdog process
1766-
# using system(1, ...) (see perlport.pod)
1792+
# using system(1, ...) (see perlport.pod). system() returns
1793+
# immediately on these platforms with effectively a pid of the new
1794+
# process
17671795
if ($is_mswin || $is_vms) {
17681796
# On Windows, try to get the 'real' PID
17691797
if ($is_mswin) {
@@ -1777,7 +1805,7 @@ ($;$)
17771805
return if ($pid_to_kill <= 0);
17781806

17791807
# Launch watchdog process
1780-
my $watchdog;
1808+
undef $watchdog;
17811809
eval {
17821810
local $SIG{'__WARN__'} = sub {
17831811
_diag("Watchdog warning: $_[0]");
@@ -1826,7 +1854,7 @@ ($;$)
18261854
}
18271855

18281856
# Try using fork() to generate a watchdog process
1829-
my $watchdog;
1857+
undef $watchdog;
18301858
eval { $watchdog = fork() };
18311859
if (defined($watchdog)) {
18321860
if ($watchdog) { # Parent process
@@ -1871,10 +1899,12 @@ ($;$)
18711899
# Use a watchdog thread because either 'threads' is loaded,
18721900
# or fork() failed
18731901
if (eval {require threads; 1}) {
1874-
'threads'->create(sub {
1902+
$watchdog_thread = 'threads'->create(sub {
18751903
# Load POSIX if available
18761904
eval { require POSIX; };
18771905

1906+
$SIG{'KILL'} = sub { threads->exit(); };
1907+
18781908
# Execute the timeout
18791909
my $time_left = $timeout;
18801910
do {

0 commit comments

Comments
 (0)