Skip to content

Conversation

@khwilliamson
Copy link
Contributor

@tonycoz noticed a simplification this makes.

If something goes wrong on WIndows and VMS, previously it gave up; now it tries an alarm.

It now doesn't try to use the thread method unless safe signals are in effect.

Some comments are clarified

  • This set of changes does not require a perldelta entry.

@tonycoz
Copy link
Contributor

tonycoz commented Oct 5, 2025

How is:

watchdog(5);
sleep 4;
watchdog(10);
sleep 8;
# eof

meant to behave?

Right now with the changes from this PR a simple test like:

#!perl
    BEGIN {
        chdir 't' if -d 't';
        require './test.pl';
        set_up_inc(qw '../lib ...');
    }

watchdog(5);
sleep 4;
watchdog(10);
sleep 8;
ok(1);
done_testing();

I get:

$ ./perl t/try.t
# Test process timed out - terminating
Killed

One option here would be for all entries to the watchdog() function to clear existing watchdogs and then return immediately after if the timeout is zero

With that the above produces:

$ ./perl t/try.t
ok 1 - [at t/try.t line 12]
1..1

I tested with:

@@ -1849,7 +1849,6 @@ sub watchdog ($;$)
 
     # If cancelling, use the state variables to know which method was used to
     # create the watchdog.
-    if ($timeout == 0) {
 
         # Is a no-op if no watchdog is currently set
         if ($watchdog_thread) {
@@ -1865,8 +1864,8 @@ sub watchdog ($;$)
         undef $watchdog_thread;
         undef $watchdog_process;
         undef $watchdog_alarm;
-        return;
-    }
+        return if $timeout == 0;
+

Parenthesize the ternary conditional for clarity

Change some indentation, mostly in preparation for later commits
In the thread documentation, I noticed that unsafe signals preclude
killing threads with a signal.  But this is the normal method for
clearing a watchdog timer under threads.  I doubt that people are
compiling with PERL_OLD_SIGNALS these days, so I didn't check for that,
but it's easy enough to check for the environment variable that does the
same thing at runtime.
Prior to this commit, the code gave up immediately if it tried
unsuccessfully to use a watchdog thread.  But the alarm method is still
available.  Drop down to try it.
The next commit changes things, so that some people may find that after
that, the current name would be misleading or confusing.
The fallback method of setting a watchdog timer is to use alarm().
Prior to this commit, the code presumed that if it wasn't one of the
other possibilities, it must be this one.  This is a valid assumption
currently, but future commits will change that.  Prepare for them with
this commit.
Only one type of watchdog timer supposedly can exist.  But I can see the
possibility that a temporary glitch in the system caused a fallback type
to be used.  This commit simply makes sure that when cancelling,
anything out there gets cancelled, instead of assuming there's just one
possibility.
This enhances the code to work on cases like:

watchdog(5);
sleep 4;
watchdog(10);
sleep 8;

Prior to this commit, you had to explicitly cancel an existing watchdog
before setting a new one.

As a result of this commit, all possible wathdog variables get undef'd
early.  This makes later undef calls redundant, so they are removed.

Suggested by Tony Cook
This commit removes the existing END blocks that vary depending on the
type of timer being used, and replaces them with a single END block that
always exists, and simply calls watchdog(0).  This already cancels
whatever watchdogs are in place, doing nothing if none are.

Spotted by Tony Cook

Even though a test file should clear every timer before exit, this makes
sure it happens.
@khwilliamson
Copy link
Contributor Author

fixed

@khwilliamson khwilliamson merged commit 1d1db97 into Perl:blead Oct 7, 2025
33 checks passed
@khwilliamson khwilliamson deleted the newwatchdog branch October 7, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants