[OpenAFS] Re: Using a file lock to avoid race conditions?

Andrew Deason adeason@sinenomine.net
Tue, 15 Mar 2011 11:39:39 -0500

On Tue, 15 Mar 2011 12:14:32 -0400
"Edgecombe, Jason" <jwedgeco@uncc.edu> wrote:

> I'm thinking of using a file lock (i.e. flock) in AFS to determine who
> fetches the email, but I need to know about the failure scenarios.
> Does the lock persist if the machine freezes or dies? Will an orphaned
> lock expire, and if so, how long does it take to expire?

It expires after five minutes. Well, a maximum of five minutes; it's
five minutes since the last time the client set/extended the lock, so it
may be less than that. This is a compile-time constant in the fileserver

> Any other suggestions are welcome.

Another way of doing this is to logically "lock" the process by creating
a file with O_CREAT | O_EXCL. That has the benefit of clients being able
to override the lock by deleting the file and attempting to recreate it,
and clients can get more information on the lock setter. That is, they
could know when the lock was last set (from the mtime), or if you write
some information to the file about the node that set the lock, you know
who set it (and/or why they set it, other information, etc). So,
whatever program you're writing can make a more informed decision on
whether the lock is stale and whether to override the lock.

One thing to keep in mind is that filesystem-level locks like flock go
away if the process holding them unexpectedly dies (or closes the file
for any other reason). Usually that's desirable, since the lock doesn't
stay held if you segfault or whatever, but if you have one process
holding the lock for something being done by another process, it may not
be a good thing.

Andrew Deason