View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Steffen Müller > File-SharedNFSLock-0.03 > File::SharedNFSLock



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.03   Source   Latest Release: File-SharedNFSLock-0.04


File::SharedNFSLock - Inter-machine locking on NFS volumes


  use File::SharedNFSLock;
  my $flock = File::SharedNFSLock->new(
    file => 'some_file_on_nfs',
  my $got_lock = $flock->lock(); # blocks for $timeout_acquire seconds if necessary
  if ($got_lock) {
    # hack hack hack...
  # meanwhile, on another machine or in another process:
  my $flock = File::SharedNFSLock->new(
    file => 'some_file_on_nfs',
  my $got_lock = $flock->lock(); # blocks for timeout or until first process is done
  # ...


NFS (at least before v4) is evil. File locking on NFS volumes is worse. This module attempts to implement file locking on NFS volumes using lock files and hard links. It's in production use at our site, but if it doesn't work for you, I'm not surprised!

Note that the lock files are always written to the same directory as the original file! There is always one lock file per process that tries to acquire the lock. This module does NOT do signal handling. You will have to do that yourself.


I use the fact that hard links are (err, appear to be) atomic even with NFS. So I write a process-specific, unique lock file and then hard-link it to the real thing. Afterwards, stat() tells me the number of hard-linked instances of the file (when polling my unique, private file). This indicates that I have acquired the lock.

The algorithm was snatched from a document called NFS Considered Harmful by Shane Kerr. I found it at Look for chapter III, List of Concerns, concern d: Exclusive File Creation. The described workaround is, I quote the above:

  The solution for performing atomic file locking using a lockfile
  is to create a unique file on the same fs (e.g., incorporating
  hostname and pid), use link(2) to make a link to the lockfile and
  use stat(2) on the unique file to check if its link count has
  increased to 2. Do not use the return value of the link() call.



Creates a new lock object but does NOT attempt to acquire the lock (see lock() below). Takes named arguments. All times in the parameters are in seconds and can be floating point values, indicating a fraction of a second.

Mandatory argument: file pointing at the file that is to be locked.

Optional arguments: poll_interval indicates the number of seconds to wait between attempts to acquire the lock. Defaults to 1 second.

timeout_acquire indicates the total time that may be spent trying to acquire a lock when lock() is called. After this time has elapsed, we bail out without having acquired a lock. Default: 60 seconds. If set to 0, the lock acquisition effectively becomes non-blocking.

timeout_stale indicates the number of seconds since the creation of an existing lock file, after which this alien lock file is to be considered stale. A stale lock will be removed and replaced with our own lock (watch out!). Default: 5 minutes. Set this to 0 to disable the feature.

unique_token is an optional parameter that will uniquely identify the lock. If you want to attempt locking the same file from the same process in different locations, they must set a unique token (process id and hostname are used additionally). Set this to 1 to have a random token auto-generated.


Attempts to acquire a lock on the file. Returns 1 on success, 0 on failure (time out).


Releases the lock, deletes the lock file. This is automatically called on destruction of the lock object!


Checks whether we have the lock on the file.

Note: Fairly expensive operation requiring a stat call.


This isn't as well tested as it should be even though it is being used in production here. Do your own testing.

There are no unit tests! (Patches welcome!)

Born out of frustration with existing locking modules.

Probably contains hidden race conditions.


File::NFSLock, but that doesn't work for multiple machines (just for a single machine and multiple processes).

Time::HiRes is used to implement fractional-second sleep() and time() calls.


Steffen Mueller, <>


Copyright (C) 2010-2011 by Steffen Mueller

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: