Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: singleton lock not reliable

by hippo (Bishop)
on Jun 23, 2021 at 22:22 UTC ( #11134223=note: print w/replies, xml ) Need Help??


in reply to singleton lock not reliable

You are trying to obtain an exclusive lock on a filehandle that you've opened for reading. I am no expert on file locking but I don't think that's possible. The FAQ is explicit is stating that the filehandle must be opened for writing (or appending or read+write) when using lockf at least. Perhaps that is the cause of your problem. The canonical example in the Monastery is Highlander - allow only one invocation at a time of an expensive CGI script and that also uses a writing filehandle.

I suggest you try switching to a writing filehandle (obviously not on $0) and see if that enables the lock for you reliably across your various OSes.


🦛

Replies are listed 'Best First'.
Re^2: singleton lock not reliable
by LanX (Sage) on Jun 24, 2021 at 09:51 UTC
    > The FAQ is explicit is stating that the filehandle must be opened for writing (or appending or read+write)

    it depends on the implementation, the perldocs refer to multiple different OS functions potentially used for flock and say

    lockf(3) does not provide shared locking, and requires that the filehandle be open for writing (or appending, or read/writing).

    So yes, it's possible that some Perl ports will fail when opening with < , if Perl was compiled to use lockf

    Personally I'm doing it deliberately on WIN (where it works) to make sure my colleagues understand that the lockfile is an empty semaphore only. (Opening for writing with > has it's own hazards, because the content will be deleted each time.)

    It really depends on the OS and FS and should be tested for each combination.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re^2: singleton lock not reliable
by ikegami (Patriarch) on Jun 24, 2021 at 04:24 UTC

    I am no expert on file locking but I don't think that's possible.

    It is.

    $ touch lock $ perl -M5.010 -MFcntl=LOCK_EX -e' open(my $fh, "<", "lock") or die $!; flock($fh, LOCK_EX) or die $!; say "ok"; ' ok

    flock locks have nothing to do with reading and writing, and they prevent neither. They merely prevent locks from being obtained.

    Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134223]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-08-08 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?