Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was testing file locking and I put a sleep(200) after a:
flock(FP, LOCK_EX) or die "Cannot lock $FILE ($!)\n".
I ran another copy of the program to test the file locking and it blocked as expected.

Then I went and Control-C's (killed) the first program (stuck in the sleep(200)). The second copy of the program finished.

Now, whenever I run the program again, the lock fails with the message:

Cannot lock filename (Bad file number)
How do I kill this lock/stop this error message?

This is perl, v5.6.1 built for sun4-solaris

Replies are listed 'Best First'.
Re: How do I kill a stuck lock on a file?
by Abigail-II (Bishop) on Oct 31, 2002 at 11:21 UTC
    Are you sure your open succeeded? It looks like you are trying to flock on a closed file handle.

    Abigail

      Yes, the open() succeeds because I have:

      open(...) or die "..."; flock(...) or die "...";
Re: How do I kill a stuck lock on a file?
by ehdonhon (Curate) on Oct 31, 2002 at 00:07 UTC

    Can you post sample code which demonstrates the problem? Whenever all of the copies of the file descriptor for the file you are working on are closed, the kernel should be cleaning up that lock for you.

      I tried creating a simple test program with open(), flock() and sleep() but I can't reproduce the error. Grrr...

      When I run the original program, though, I still get the same error. All the hosts are NFS mounting the filesystem on which the file I'm trying to lock, reside.

      This could posssibly be a bug in Solaris8 since if I run the same program under Linux, it runs without the error.