in reply to race condition? - flock and $SIG{ALRM}

Thanks to the recent input I've recieved - much appreciated.

I hope replying directly to my own node isn't considered too horribly faux pas - I'd like to clarify some things...

The transaction gig will not be a possible solution to this particular project however, so I'm stuck with doing the best I can protecting a single file that will be appended to very often by multiple processes - these processes will all be separate instances of the same program. This program, called rtmail, is a mail filter that gets piped to from sendmail.

What rtmail does is route mail to final destinations, depending upon the sender's domain. Thats easy - it's the bit where rtmail also makes a backup of all mail sent to it on the local machine using different spool files, named accordingly to each of the final remote destinations, that is the tricky part... This is mission critical stuff, and so I want to be as certain as possible that it's all being reliably and accurately backed off into these local mail spools.

One thing I didn't mention in my post, is that most of those die()'s are actually replaced with a call to an exception handler I wrote that makes certain absolutely no mail, under any circumstance is ever to simply disappear into the void.

So, what I've learned so far from your replies is that:

Perl signal traps are unreliable I really need the timeout though, so if something goes terribly wrong, I don't have a seething mass of rtmails filling the process table on the mail server, all waiting indefinately for a lock...

The race condition between the successful flock and the alarm unset is possible, but not to be worried about If that's the case, then I could simply bail on the inner eval and not ever bother with the extra checks on $@ . And when/if the race condition ever does occur, so what - it'll just get caught by my exception handler as if the flock timed out, so no actual damage or lost data will take place anyhow. But I would prefer, if possible, to not raise the exception routine if it's not truly an exception situation - perhaps I'm just being too retentive...

Thanks for everyones insight, I really appreciate the help. Beers!

  • Comment on RE: race condition? - flock and $SIG{ALRM}

Replies are listed 'Best First'.
(atl: about exceptions) RE: RE: race condition? - flock and $SIG{ALRM}
by atl (Pilgrim) on Sep 18, 2000 at 14:35 UTC
    Thus spoke Zarathustra:
    One thing I didn't mention in my post, is that most of those die()'s are actually replaced with a call to an exception handler I wrote ...

    Just a short note: if you catch the exception instead of letting the process die, youŽll have to close the file yourself to get rid of the lock. Otherwise you might block other processes in the (rather rare) case the race condition occurs.

    As for using an exception or not ... if you need this file lock to continue normally, not getting it (including timeout) should be considered an exceptional condition. But thatŽs a matter of programming style, certainly no "rigth" or "wrong" way ...

    Beers! Yup! :-))

    Andreas

      if you catch the exception instead of letting the process die, youŽll have to close the file yourself to get rid of the lock. Otherwise you might block other processes in the (rather rare) case the race condition occurs.

      Hey, thanks - you're absolutely right. That's one bit I had completely over looked...

      Good eye!

        Just have the filehandle be localized or in a lexical scope. Then it will close on its own when it notices that you exited the scope it was opened in. :-)
off topic ( RE: RE: race condition? - flock and $SIG{ALRM} )
by Zarathustra (Beadle) on Sep 17, 2000 at 23:27 UTC

    Ah well, I'm new here - guess it shows. (c8=