@@ -1733,8 +1733,34 @@ sub warning_like {
1733
1733
# _AFTER_ the 'threads' module is loaded.
1734
1734
sub watchdog ($;$)
1735
1735
{
1736
+ CORE::state $watchdog ;
1737
+ CORE::state $watchdog_thread ;
1736
1738
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
+
1738
1764
my $timeout_msg = ' Test process timed out - terminating' ;
1739
1765
1740
1766
# Accept either spelling
@@ -1763,7 +1789,9 @@ ($;$)
1763
1789
if (!$threads_on || $method eq " process" ) {
1764
1790
1765
1791
# 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
1767
1795
if ($is_mswin || $is_vms ) {
1768
1796
# On Windows, try to get the 'real' PID
1769
1797
if ($is_mswin ) {
@@ -1777,7 +1805,7 @@ ($;$)
1777
1805
return if ($pid_to_kill <= 0);
1778
1806
1779
1807
# Launch watchdog process
1780
- my $watchdog ;
1808
+ undef $watchdog ;
1781
1809
eval {
1782
1810
local $SIG {' __WARN__' } = sub {
1783
1811
_diag(" Watchdog warning: $_ [0]" );
@@ -1826,7 +1854,7 @@ ($;$)
1826
1854
}
1827
1855
1828
1856
# Try using fork() to generate a watchdog process
1829
- my $watchdog ;
1857
+ undef $watchdog ;
1830
1858
eval { $watchdog = fork () };
1831
1859
if (defined ($watchdog )) {
1832
1860
if ($watchdog ) { # Parent process
@@ -1871,10 +1899,12 @@ ($;$)
1871
1899
# Use a watchdog thread because either 'threads' is loaded,
1872
1900
# or fork() failed
1873
1901
if (eval {require threads; 1}) {
1874
- ' threads' -> create(sub {
1902
+ $watchdog_thread = ' threads' -> create(sub {
1875
1903
# Load POSIX if available
1876
1904
eval { require POSIX; };
1877
1905
1906
+ $SIG {' KILL' } = sub { threads-> exit(); };
1907
+
1878
1908
# Execute the timeout
1879
1909
my $time_left = $timeout ;
1880
1910
do {
0 commit comments