Subject: | Proc::PID::File should not die on all flock errors |
Date: | Wed, 21 Dec 2016 17:16:14 -0800 |
To: | bug-Proc-PID-File [...] rt.cpan.org |
From: | Rubin <rubin [...] afternet.org> |
Hi,
I'm trying to use Proc::PID::File to discover if I a script is already
running or not. It should return 0, or the pid of the running process, but
I have found that sometimes instead the script just dies with a 'Resource
temporarily unavailable' error. This seems to be the way this error is
handled from flock().
The error seems to happen when the timing is JUST right so that 2 copies of
the script try to create & lock the pid file at the same exact instant. The
result is that instead of Proc::PID::File->running() (or alive) returning
true or not true, the script will just die() unexpectedly.
It is very rare under normal circumstances making it hard to debug, but
here is a concocted example script that shows the problem:
Run this script 10 times at once in bash; In rare cases, an instance of the
script will exit without executing either "Running..." print statement. (IE
if I run 10 copies at once in bash, I should see "Running Yes/No" 10 times
but instead sometimes I will see only 9 + an error).
Execute it as: ./flocktest.pl &./flocktest.pl &./flocktest.pl &./
flocktest.pl &./flocktest.pl &./flocktest.pl &./flocktest.pl &./flocktest.pl
&./flocktest.pl &./flocktest.pl
----------- flocktest.pl ----------------------
#!/usr/bin/perl
use strict;
use warnings;
use Proc::PID::File;
my $waitcount = 0;
# Check we arent running twice at once
while(Proc::PID::File->running( dir => '/tmp' )) {
if($waitcount++ > 90) {
print "Running Yes: Pid file exists, tried $waitcount times,
could not get a lock\n";
# do something important here before dieing
exit 3;
}
select undef, undef, undef, 0.1;
}
print "Running No: Got the lockfile ourselves\n";
exit 0;
------------------------------------------
I'm not sure what the solution is, perhaps just returning true when flock()
fails, or maybe retrying it a few times before dieing? Or maybe just a
warning in the documentation that it could die() under 'normal' conditions.
Thanks,
--
-Alex