Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Vexing Race Condition

by tybalt89 (Monsignor)
on May 02, 2025 at 21:42 UTC ( [id://11164916]=note: print w/replies, xml ) Need Help??


in reply to Re: Vexing Race Condition
in thread Vexing Race Condition

Or use Path::Tiny's trick: open the file for append, lock it, truncate to 0, then write new data :)

( Untested, let me know if it works :)

Replies are listed 'Best First'.
Re^3: Vexing Race Condition
by Maelstrom (Beadle) on May 03, 2025 at 13:45 UTC
    Actually Path:Tiny's trick is renaming a temporary file. My idea of opening read-write, locking then truncating turns out to be a non starter as mode +> clobbers the file and mode +< fails if the file doesn't already exist, I was able to run the script with no errors after touching the file although in the wild that would be another race condition. Opening the file in append mode as you suggest does work, so it's an option although using rename is probably more robust as it will also work for processes that don't use flock.

      Check out Path::Tiny::append with the truncate option. Path::Tiny has more than one trick up its sleeve.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2025-11-17 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.