in reply to Can flock occasionally fail on systems that support file locking?

man 2 flock lists only one error condition:

EWOULDBLOCK The file is locked and the LOCK_NB flag was selected.
As cfreak suggests, some process (maybe yours) already has the lock.

Seeing your reply to cfreak, yes, a cgi environment may well be running more than one instance of your script. If LOCK_NB is not set, one instance will wait until the other lets go.

Update: Calling flock as flock FH, $opts or warn $!; will tell you what error is set. I don't understand why LOCK_NB is necessary to you. For most purposes, the blocking form is preferred. The effect is to sleep until the file is available.

After Compline,
Zaxo

  • Comment on Re: Can flock occasionally fail on systems that support file locking?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Can flock occasionally fail on systems that support file locking?
by rzward (Monk) on Jul 10, 2003 at 15:06 UTC
    Thank you for your reply.

    Yes, I have read the man pages but am wondering if in practice there are occasions when there are other reasons for flock to fail.

    "...If LOCK_NB is not set, one instance will wait until the other lets go."

    This is what I expect to happen and how I have designed the script to work.

    Unfortunately, I'm getting reports that occasionally a submission isn't making it to the data file and I'm wondering if flock is failing in these cases, thus allowing the data file to be opened, written to and closed at the same time by more than one user.

    Adding error handling around flock might confirm my suspicion -- but I also wonder if flock occasionally succeeds for a second simultaneous submission because of lock inheriting or something even more mind boggling.

    Richard