Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Strawberry Perl and alarm() on Windows

by SimonPratt (Friar)
on May 22, 2015 at 16:52 UTC ( [id://1127487]=note: print w/replies, xml ) Need Help??


in reply to Re: Strawberry Perl and alarm() on Windows
in thread Strawberry Perl and alarm() on Windows

Uhh, am I missing something?

use strict; use warnings; print scalar localtime, "\n"; eval { $SIG{__DIE__} = sub { alarm(0) }; $SIG{ALRM} = sub { die "timeout" }; alarm(2); system('sleep 5'); }; print $@ if $@; print scalar localtime, "\n";

Output:

Fri May 22 17:46:53 2015
timeout at C:\Temp\b.plx line 8.
Fri May 22 17:46:55 2015

Alarm appears to work just fine even when calling out to system

Replies are listed 'Best First'.
Re^3: Strawberry Perl and alarm() on Windows
by afoken (Chancellor) on May 24, 2015 at 09:21 UTC
    am I missing something?

    Yes. I did not write about the system function, but about the system (as in "operating system"). I cited alarm in perlport: "Emulated using timers that must be explicitly polled whenever Perl wants to dispatch "safe signals" and therefore cannot interrupt blocking system calls. (Win32)" (Emphasis mine) I wrote " Replace sleep with something that blocks for 5 seconds outside perl (i.e. in the system) and alarm will no longer work."

    Demo, using flock, a function using a system call that may block for a long time:

    #!/usr/bin/perl use strict; use warnings; use v5.10; use Fcntl qw( LOCK_EX LOCK_UN ); use autodie qw( open flock close ); sub main { # create a tempfile open my $h,'>>','tempfile.tmp'; close $h; # start a background process that locks the tempfile for 10 second +s if ($^O eq 'MSWin32') { system 1,$^X,$0,'locker'; } else { my $pid=fork() // die "Can't fork: $!"; unless ($pid) { exec $^X,$0,'locker' or die "Can't exec: $!"; } } sleep 1; # wait one second for the helper process; open $h,'>>','tempfile.tmp'; $@=''; my $start=time(); eval { local $SIG{'ALRM'}=sub { die "timeout" }; say 'main: alarm 5'; alarm(5); say 'main: flock LOCK_EX'; flock($h,LOCK_EX); say 'main: alarm 0'; alarm(0); }; my $err=$@ || 'successfully locked'; my $stop=time(); say "main: $err"; say 'main: ',$stop-$start,' seconds have passed'; close $h; # allow locker() to finish before returning to command prompt ($^O eq 'MSWin32') ? sleep 1 : wait; } sub locker { open my $h,'>>','tempfile.tmp'; say 'locker: flock LOCK_EX'; flock($h,LOCK_EX); say 'locker: locked'; say 'locker: sleep 10'; sleep 10; say 'locker: flock LOCK_UN'; flock($h,LOCK_UN); say 'locker: unlocked'; close $h; } @ARGV ? locker() : main();

    How the demo works:

    Without arguments, main() is invoked, main() creates a background process (using system(1,@args) on Windows, fork() and exec() on Linux), waits a second, then tries to lock a temp file with a classic timeout construct (eval, $SIG{'ALRM'}=sub { die }, alarm($timeout), system call, alarm(0)). Time for this is measured. The background process is the same script, but invoked with an argument, so that locker() is invoked instead of main(). Locker locks the temp file for 10 seconds, and because main() waits a second, it will succeed. In main(), the temp file is already locked, so flock() will block until locker() releases the lock OR the system call is interrupted by the ALRM signal caused by alarm().

    On Linux, signals just work. flock() in main() is interrupted by the ARLM signal, the signal handler in $SIG{'ALRM'} is invoked, that terminales the eval block using die() after 5 seconds.

    On Windows, signals are emulated. alarm($timeout) sets up a timer that must be polled. This is impossible during a blocking system call like flock(). So flock() blocks until it can aquire the lock after about 9 seconds (locker() waits 10 seconds after locking, main() waits 1 second before trying to lock, 9 seconds remain). $SIG{'ALRM'} is not invoked. alarm(0) disables the timer. The eval block terminates without an error.

    Output on Linux:

    >perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX main: timeout at lockdemo.pl line 30. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked >

    Output on Windows:

    H:\tmp>perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX locker: flock LOCK_UN main: alarm 0 main: successfully locked main: 10 seconds have passed locker: unlocked H:\tmp>

    So why did alarm($timeout) "work" with system "sleep 10"?

    system @args (unlike system 1,@args) waits for the sub-process to terminate in perl. (flock() waits in the operating system!) During that time, perl can poll the timer and thus can emulate the ARLM signal.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      On Windows, signals are emulated. alarm($timeout) sets up a timer that must be polled.

      That's not a very good description of what actually happens.

      On Windows, alarm is simulated using the SetTimer() system API, which send a message to the process' message queue when the time expires.

      From perl5.18/Win32.c:

      And that message is received when the run loop checks the message queue:

      And, at least since the advent of SAFE_SIGNALS in 5.8.1, *nix perls, also only process signals, between opcodes, by "polling" (from the run loop) to see if any have been received during the last opcode. Ie. in exactly the same way as Windows perls do.

      The reason your rather over-elaborate demo "works" on *nix and not under windows, is because under *nix, signals are also delivered (by the OS) to child processes, whereas they are obviously not on Windows. Thus, the locker child process also receives the alrm signal which interrupts the 10 second sleep, thus the lock it holds gets removed, and the main() process can therefore acquire its lock before the alarm is triggered.

      This is clearly shown by the output you posted:

      >perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX ### The lock is acquired +here main: timeout at lockdemo.pl line 30. ### before the timeout oc +curs. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked

      Perl's "Safe Signals" (unless disabled) are also implemented on *nix perls, and are only acted upon between opcodes, just as they are on Windows perls. Your demo is deceptive (I'm not suggesting deliberately so), because the alarm is not interrupting the flock opcode in the main() process, but the sleep in the child process. The lock is only acquired early in the main process because the child process relinquished its lock early.

      That doesn't happen in the Windows example, because the OS doesn't do signals, thus doesn't deliver the signal to the child process.

      I agree that the emulated signals on Windows are less complete than the real ones on *nix; but then the reason Perl has "Safe signals" in the first place is because the entire concept of using asynchronous interrupts as a flow-control mechanism is terminally brain-dead.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        This is clearly shown by the output you posted:

        >perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX ### The lock is acquired +here main: timeout at lockdemo.pl line 30. ### before the timeout oc +curs. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked
        download

        Sorry, either I don't understand what you wrote here or you don't understand what happens in my code when it runs on Linux. (Did you actually run it on a Linux system?)

        The output line "main: flock LOCK_EX" is written before flock() is called in main(). That does not aquire a lock. The next output line "main: timeout at lockdemo.pl line 30." is written by code following eval, the timeout comes from the signal handler in line 30. If flock() was successful, the next code line should have written "main: alarm 0" to the output - unless, of course, the ALRM signal was handled right between flock() and say(). For a successful flock(), locker() must have unlocked the file. That takes at least 10 seconds due to sleep(10) in locker(). After 5 seconds (plus one second due to the earlier sleep(1) in main()), the file is still locked and flock() in main() must have failed.

        (I know that the file was still locked for another 4 seconds when the timeout occurred, see below.)

        the alarm is not interrupting the flock opcode in the main() process, but the sleep in the child process. The lock is only acquired early in the main process because the child process relinquished its lock early.

        Your argument is that locker() was killed by SIGALRM set up by alarm(5) in main(). By the time that alarm(5) is called in main(), about 1 second should have passed due to sleep 1. Enough time for locker() to run into sleep(10). If locker was killed right there, the following output ("locker: flock LOCK_UN" and "locker: unlocked") would not exist. But because it exists, we can be sure that locker() was not killed. BUT ...

        The Linux manual page for sleep warns not to mix sleep and alarm, because sleep() may be implemented using SIGALRM. (Emphasis mine) So perhaps that SIGALRM messed up sleep() (i.e. sleep() in locker caught SIGALRM and slept much less than 10 seconds)?

        I changed locker() to measure time as well:

        sub locker { my $start=time(); open my $h,'>>','tempfile.tmp'; say 'locker: flock LOCK_EX'; flock($h,LOCK_EX); say 'locker: locked'; say 'locker: sleep 10'; sleep 10; say 'locker: flock LOCK_UN'; flock($h,LOCK_UN); say 'locker: unlocked'; close $h; my $stop=time(); say 'locker ran for ',$stop-$start,' seconds'; }

        Output on Linux is:

        >perl lockdemo-timed.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX main: timeout at lockdemo-timed.pl line 30. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked locker ran for 10 seconds >

        Output on Windows is:

        H:\tmp\lockdemo>perl lockdemo-timed.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX locker: flock LOCK_UN main: alarm 0 main: successfully locked main: 9 seconds have passed locker: unlocked locker ran for 10 seconds H:\tmp\lockdemo>

        No change compared to the original output, except for the filename and the final "locker ran for 10 seconds" line.

        So no, locker() is not affected by SIGALRM at all. (This is on perl 5.18.1, Slackware 14.1 64 bit, Linux 3.10.17) If locker() was disturbed by alarm(5) from main(), that would happen after about 6 seconds, resulting in a total runtime of about 6 seconds, not 10 seconds. If locker() was killed by alarm(5) from main(), the total run time would not be written to the output.

        To avoid any issues with SIGALRM and sleep, let's explicitly ignore SIGALRM and change sleep($timeout) to select(undef,undef,undef,$timeout):

        sub locker { $SIG{'ALRM'}='IGNORE'; my $start=time(); open my $h,'>>','tempfile.tmp'; say 'locker: flock LOCK_EX'; flock($h,LOCK_EX); say 'locker: locked'; say 'locker: sleep 10 using select'; select(undef,undef,undef,10); say 'locker: flock LOCK_UN'; flock($h,LOCK_UN); say 'locker: unlocked'; close $h; my $stop=time(); say 'locker ran for ',$stop-$start,' seconds'; }

        Output on Linux:

        >perl lockdemo-select.pl locker: flock LOCK_EX locker: locked locker: sleep 10 using select main: alarm 5 main: flock LOCK_EX main: timeout at lockdemo-select.pl line 30. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked locker ran for 10 seconds >

        Output on Windows:

        H:\tmp\lockdemo>perl lockdemo-select.pl locker: flock LOCK_EX locker: locked locker: sleep 10 using select main: alarm 5 main: flock LOCK_EX locker: flock LOCK_UN main: alarm 0 main: successfully locked main: 9 seconds have passed locker: unlocked locker ran for 10 seconds H:\tmp\lockdemo>

        Again, no changes compared to the original output, except for the filename and the final locker ran for 10 seconds line.

        The reason your rather over-elaborate demo "works" on *nix and not under windows, is because under *nix, signals are also delivered (by the OS) to child processes

        I doubt that alarm(timeout) sends a signal to child processes. The Linux man page for alarm states: " alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds. ". No word about sending signals to child processes. POSIX has a similar statement: "The alarm() function shall cause the system to generate a SIGALRM signal for the process after the number of realtime seconds specified by seconds have elapsed." And it explicitly specifies what happends to child processes: "The fork() function clears pending alarms in the child process. A new process image created by one of the exec functions inherits the time left to an alarm signal in the old process' image."

        Signals are sent to only one process by default when using kill, though it is possible to send signals to process groups by using a negative process ID as argument to kill:

        The kill() system call can be used to send any signal to any process group or process. If pid is positive, then signal sig is sent to the process with the ID specified by pid. If pid equals 0, then sig is sent to every process in the process group of the calling process. If pid equals -1, then sig is sent to every process for which the call‐ ing process has permission to send signals, except for process 1 (init), but see below. If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.

        (From the kill(2) Linux man page, POSIX is very similar.)

        Let's see if a signal is delivered to locker(), as you suggest:

        sub locker { $SIG{'ALRM'}=sub { warn "*** ALARM ***" }; my $start=time(); open my $h,'>>','tempfile.tmp'; say 'locker: flock LOCK_EX'; flock($h,LOCK_EX); say 'locker: locked'; say 'locker: sleep 10 using select'; select(undef,undef,undef,10); say 'locker: flock LOCK_UN'; flock($h,LOCK_UN); say 'locker: unlocked'; close $h; my $stop=time(); say 'locker ran for ',$stop-$start,' seconds'; }

        Output (Linux only):

        >perl lockdemo-signal.pl locker: flock LOCK_EX locker: locked locker: sleep 10 using select main: alarm 5 main: flock LOCK_EX main: timeout at lockdemo-signal.pl line 30. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked locker ran for 10 seconds >

        No traces of a SIGALRM here.

        So, my code works on Linux as I explained. locker() is not at all affected by alarm(5) in main.

        For better portability, maybe I should have set $SIG{'ARLM'}='IGNORE' in locker() and used select(undef,undef,undef,10) instead of sleep(10) in locker().

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1127487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-03-28 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found