Subject: | Race conditions possible |
File::Lockfile is not guarded against race conditions. The attached
sample script, which is almost the same as the code in File::Lockfile's
SYNOPSIS, can demonstrate how this race condition can be triggered. Just
start the script in two terminals within a short period (<1s). The
"sleep 1" is only there to make the simulation easier.
The problem is fundamental: the SYNOPSIS suggests to run the check
method first, and then the write method. This can only work if check and
write would be atomic, but this is not the case here. I don't have a
idea if this can be easily fixed, without using OS support like flock()
or so.
Regards,
Slaven
Subject: | race.pl |
#!/usr/bin/perl
use File::Lockfile;
my $lockfile = File::Lockfile->new('programname.pid',
'/tmp');
if ( my $pid = $lockfile->check ) {
print "Seems that programname is already running with PID: $pid\n";
exit;
}
sleep 1; # simulating slowness
$lockfile->write;
print "Process is running\n";
sleep 99999;