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.