Skip to content

Commit 8192cfd

Browse files
committed
test.pl: Use END blocks to cancel watchdog timers
In doing follow up work to a7f3f23 "Turn off watchdog when done in tests", I realized that my solution was suboptimal, and that the code already existed in test.pl to do things better. What that code does is to create an END block to cancel the watchdog upon program exit. I think that is a better solution than to force the addition of explicit calls to watchdog(0). This commit creates a standard way to specify the code that does the cancellation, and to use it, not only when the END block does, but also when the timer is explicitly cancelled. This means the calls to watchdog(0) that occur at the end of the file that were added in a7f3f23 aren't necessary. The END block takes care of it. The reason to keep them is if we want to add a porting test that every watchdog is cleared.
1 parent bb13a22 commit 8192cfd

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

t/test.pl

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,8 +1829,7 @@ sub warning_like {
18291829
# NOTE: If the test file uses 'threads', then call the watchdog() function
18301830
# _AFTER_ the 'threads' module is loaded.
18311831
{ # Closure
1832-
my $watchdog;
1833-
my $watchdog_thread;
1832+
my $cancel_string;
18341833

18351834
sub watchdog ($;$)
18361835
{
@@ -1839,29 +1838,18 @@ ($;$)
18391838
# If cancelling, use the state variables to know which method was used to
18401839
# create the watchdog.
18411840
if ($timeout == 0) {
1842-
if ($watchdog_thread) {
1843-
$watchdog_thread->kill('KILL');
1844-
undef $watchdog_thread;
1845-
}
1846-
elsif ($watchdog) {
1847-
kill('KILL', $watchdog);
1848-
undef $watchdog;
1849-
}
1850-
else {
1851-
alarm(0);
1852-
}
1853-
1841+
eval $cancel_string if defined $cancel_string;
18541842
return;
18551843
}
18561844

1857-
# Make sure these aren't defined.
1858-
undef $watchdog;
1859-
undef $watchdog_thread;
1860-
18611845
my $method = shift || "";
18621846

18631847
my $timeout_msg = 'Test process timed out - terminating';
18641848

1849+
# Common to all cancellation types; HERE gets replaced by the individual
1850+
# code
1851+
$cancel_string = 'local $! = 0; local $? = 0; HERE; undef $cancel_string';
1852+
18651853
# Accept either spelling
18661854
my $timeout_factor = $ENV{PERL_TEST_TIME_OUT_FACTOR}
18671855
|| $ENV{PERL_TEST_TIMEOUT_FACTOR}
@@ -1886,6 +1874,7 @@ ($;$)
18861874
# Don't use a watchdog process if 'threads' is loaded -
18871875
# use a watchdog thread instead
18881876
if (!$threads_on || $method eq "process") {
1877+
my $watchdog;
18891878

18901879
# On Windows and VMS, try launching a watchdog process
18911880
# using system(1, ...) (see perlport.pod). system() returns
@@ -1904,7 +1893,6 @@ ($;$)
19041893
return if ($pid_to_kill <= 0);
19051894

19061895
# Launch watchdog process
1907-
undef $watchdog;
19081896
eval {
19091897
local $SIG{'__WARN__'} = sub {
19101898
_diag("Watchdog warning: $_[0]");
@@ -1945,22 +1933,21 @@ ($;$)
19451933
return;
19461934
}
19471935

1948-
# Add END block to parent to terminate and
1949-
# clean up watchdog process
1950-
eval("END { local \$! = 0; local \$? = 0;
1951-
wait() if kill('KILL', $watchdog); };");
1936+
# Add END block to parent to terminate and clean up watchdog
1937+
# process
1938+
$cancel_string =~ s/HERE/wait() if kill('KILL', $watchdog)/;
1939+
eval("END { $cancel_string };");
19521940
return;
19531941
}
19541942

19551943
# Try using fork() to generate a watchdog process
1956-
undef $watchdog;
19571944
eval { $watchdog = fork() };
19581945
if (defined($watchdog)) {
19591946
if ($watchdog) { # Parent process
1960-
# Add END block to parent to terminate and
1961-
# clean up watchdog process
1962-
eval "END { local \$! = 0; local \$? = 0;
1963-
wait() if kill('KILL', $watchdog); };";
1947+
# Add END block to parent to terminate and clean up watchdog
1948+
# process
1949+
$cancel_string =~ s/HERE/wait() if kill('KILL', $watchdog)/;
1950+
eval("END { $cancel_string };");
19641951
return;
19651952
}
19661953

@@ -1998,7 +1985,7 @@ ($;$)
19981985
# Use a watchdog thread because either 'threads' is loaded,
19991986
# or fork() failed
20001987
if (eval {require threads; 1}) {
2001-
$watchdog_thread = 'threads'->create(sub {
1988+
my $watchdog_thread = 'threads'->create(sub {
20021989
# Load POSIX if available
20031990
eval { require POSIX; };
20041991

@@ -2022,6 +2009,9 @@ ($;$)
20222009
kill($sig, $pid_to_kill);
20232010
});
20242011

2012+
$cancel_string =~ s/HERE/$watchdog_thread->kill('KILL')->detach()/;
2013+
eval "END { $cancel_string }";
2014+
20252015
# Don't proceed until the watchdog has set up its signal handler.
20262016
# (Otherwise there is a possibility that we will exit with threads
20272017
# running.) The watchdog tells us that the handler is set by
@@ -2049,6 +2039,9 @@ ($;$)
20492039
my $sig = $is_vms ? 'TERM' : 'KILL';
20502040
kill($sig, $pid_to_kill);
20512041
};
2042+
2043+
$cancel_string =~ s/HERE/alarm(0)/;
2044+
eval "END { $cancel_string }";
20522045
}
20532046
}
20542047
} # End closure

0 commit comments

Comments
 (0)