in reply to Question about flock and printing to file

You may be suffering from buffering, and from misuse of flock. Update: This assertion is incorrect; Perl handles it correctly now.

LOCK_UN unlocks the file, removing the advisory lock before you closed the file. Remember that close flushes the output buffer if there's anything still in it. So it's entirely possible that your lock has been removed before the buffer gets flushed.

The solution is to not call flock($fh, LOCK_UN) or ..., but rather, to call close $fh or ..., at which time the lock will be cleared after flushing.

If this doesn't clear up your problem, then you have another bug in addition to the one demonstrated in the code you provided.


Dave

Replies are listed 'Best First'.
Re^2: Question about flock and printing to file
by Anonymous Monk on Jun 23, 2015 at 15:25 UTC

    flock:

    To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE before locking or unlocking it.

      Well goodness, and that's been around awhile, it would seem. :) Thanks.


      Dave