You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Justin Luster <ju...@sawtoothsoftware.com> on 2007/04/13 19:50:41 UTC

Lock Files - File is permanently locked

Hi,
 
I've read through some of the documentation on perl.apache.org about
file locking and mod_perl.  I believe that I'm following the advice
there but I'm still having problems.
 
I have individual data files that processes write to.  I've decided to
follow Dominus's advice
(http://perl.plover.com/yak/flock/samples/slide011.html ) to have an
extra lock file that is locked before writing to the actual data file.
 
I cannot reproduce the error consistently but from time to time one of
my lock files gets locked and stays locked.  I'm running Mod_Perl on a
Linux Machine.

(SERVER_SOFTWARE => Apache/1.3.27 (Unix) (Red-Hat/Linux)
FrontPage/5.0.2.2623 mod_python/2.7.8 Python/1.5.2 mod_ssl/2.8.12
OpenSSL/0.9.6b DAV/1.0.3 mod_perl/1.26 mod_webapp/1.2.0-dev)
 
As per the advice of perl.apache.org I'm using lexically scoped
variables (created with my ()) to create these lock files.  So I'm
confused as to why any lock would hang around.  Here is an example of
how I'm opening files:
 
sub OpenFile
{
my $FileHandle = Symbol::gensym();
 
open $FileHandle, $strOpenChar . $strFileName or eval{$blnError = 1};
 
return $FileHandle;
}
 
The $FileHandle is then explicitly closed in the calling code.  And even
if it is not, and I've made an error, once it goes out of scope the file
handle and the lock should be closed out.
 
Here is an example of my flock code:
 
sub LockMe
{
            my ($FileHandle, $blnExclusive) = @_;
 
            my $blnFlockSupported = 1;
 
            eval
            {
                        if ($blnExclusive) 
                        {
                                    #Lock for exclusive write access.
                                    flock $FileHandle, 2;
                        }
                        else
                        {
                                    #Lock it as a shared for multiple
readers, but it will not let a writer in.
                                    flock $FileHandle, 1;
                        }
            };
            if ($@) 
            {
                        $blnFlockSupported = 0;
                        $authlib::blnGlobalFlockSupported = 0;
            }
            
            #Another process might have changed the file position.
            seek $FileHandle, 0, 0;
 
            return $blnFlockSupported;
}
 
 
Does anyone know what might be happening?  We are only using
Apache::Registry in this instance.  I can't see how a lexically scoped
file handle that is being locked is not being unlocked once the process
ends.
 
Thanks for any help,
 
Justin

RE: Lock Files - File is permanently locked

Posted by Justin Luster <ju...@sawtoothsoftware.com>.
According to the modperl documentation:

http://perl.apache.org/docs/1.0/guide/debug.html#Safe_Resource_Locking_a
nd_Cleanup_Code

The simplest solution to this problem is to always use lexically scoped
variables (created with my ()). Whether script gets aborted before
close() is called or you forgot the use close() the lexically scoped
variable will always go out of scope and therefore if the file was
locked it will be unlocked. Here is a good version of the code:

  flock4.pl
  ---------
  use Fcntl qw(:flock);
  use Symbol ();
  my $fh = Symbol::gensym();
  open $fh, "+>>filename" or die "$!";
  flock $fh, LOCK_EX;
    # do something
  close $fh;

Notice they do not mention using LOCK_UN.  Also Dominus says that "
Using LOCK_UN is almost always a mistake"

http://perl.plover.com/yak/flock/samples/slide005.html

Any other ideas?


-----Original Message-----
From: Robert Landrum [mailto:rlandrum@aol.net] 
Sent: Friday, April 13, 2007 1:07 PM
To: Justin Luster
Cc: modperl@perl.apache.org
Subject: Re: Lock Files - File is permanently locked

Justin Luster wrote:
> Does anyone know what might be happening?  We are only using 
> Apache::Registry in this instance.  I can't see how a lexically scoped

> file handle that is being locked is not being unlocked once the
process 
> ends.

The process isn't ending if you're using Apache::Registry.

I think you'll need to

   use Fcntl qw(:flock);
   flock $FileHandle, LOCK_UN;

unless you set the Max child process config option to 1 (in which case, 
apache::registry would be useless).

Also, if you're using child locking like this, you can write the 
requesting process's PID to the .lock (or whatever) file.  Then a cron 
job could come along and reap your .lock's if the PID is no longer
running.

Then your file would only ever be locked for at most a minute.

Seems like overkill though.

Rob






Re: Lock Files - File is permanently locked

Posted by Robert Landrum <rl...@aol.net>.
Justin Luster wrote:
> Does anyone know what might be happening?  We are only using 
> Apache::Registry in this instance.  I can’t see how a lexically scoped 
> file handle that is being locked is not being unlocked once the process 
> ends.

The process isn't ending if you're using Apache::Registry.

I think you'll need to

   use Fcntl qw(:flock);
   flock $FileHandle, LOCK_UN;

unless you set the Max child process config option to 1 (in which case, 
apache::registry would be useless).

Also, if you're using child locking like this, you can write the 
requesting process's PID to the .lock (or whatever) file.  Then a cron 
job could come along and reap your .lock's if the PID is no longer running.

Then your file would only ever be locked for at most a minute.

Seems like overkill though.

Rob