Berkeley CSUA MOTD:Entry 44658
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2024/11/23 [General] UID:1000 Activity:popular
11/23   

2006/10/4-5 [Computer/SW/Languages/Perl] UID:44658 Activity:kinda low
10/3    Does anyone know why using DB_File in perl would suddenly stop working
        and start storing only blank entries into the DB?
        The code is something like this (there can be multiple instances of
        this perl script running hence the lockfile)

        open (LOCKFILE, ">lock_file");
        flock (LOCKFILE, $LOCK_EX);
        dbmopen (%PERLDB, db_file, 0666)
        set values in %PERLDB
        flock (LOCKFILE, $LOCK_UN);
        close (LOCKFILE)
        dbmclose (%PERLDB);

        So once in a while if I set a value in %PERLDB it just sets it to
        blank (nothing) instead of the right value.  I've tried checking the
        value afterwords, and setting it over and over again until it sticks,
        but that eventually leads to Too Many Files open.  Is there a way
        to check file handle leaking in perl?  I don't see a place where I'm
        opening a handle without closing it in my script ...
        \_ You need to do the dbmclose before you unlock the lockfile.
           \_ good catch
        \_ Maybe you're hitting a race condition between the open() and the
           flock().  -tom
           \_ I think so too if that's what the code actually looks like.
              OP, why do you open()/close() the lock file at all?  Why not
              just lock/unlock it?  Can you show us the actual production
              code block?
              \_ I commented out the flock and opens and the problem didn't
                 go away.  It turns out the open was a braindead way of making
                 sure that file existed, I'll fix that too.
                 \_ Without locking and multiple instances of this code
                    running I would expect Bad Things(tm) to happen.  You do
                    want locking of some sort.  You don't need the open().
                    I'm left wondering if your file is randomly named or is
                    really the static "lockfile" in your code.  Again, if you
                    can post the real production code you'll probably get
                    better help than this sort of vague hand waving.
                        \_ Actually I take what I said back, the flock/open
                           is correct, at least according to examples on how
                           to lock a file.  How do you do a flock on a file
                           that you haven't opened?
                           \_ My error.  In perl you do have to open the file
                              to get a filehandle for flock.  Anyway, perl
                              relies on your OS's underlying locking mechanism
                              which may not even exist and you should really
                              be testing the return value, not assume it has
                              been successfully granted a lock.  If you test
                              the return value you may find you're not getting
                              a lock which is why your code behaves the same
                              with and without the flock().