I have multiple instances of a script that will need to write on a single file, so I was looking into locking the access to said file by using mkpidlock. Eventually I’ll want to use it with HDF5 files (which I know I can’t with mkpidlock as it is) so I’m trying to understand its inner workings.
I think there’s something fundamental I’m not understanding, since the following behaviour surprises me:
- Create a plain text file,
echo "hello" > test.txt - Start Julia, loading the function:
julia -ie 'using FileWatching.Pidfile: mkpidlock' - Open the file with it, but without doing anything with it:
mkpidlock("test.txt"; stale_age=2, wait=true, mode=0o666) do
() -> nothing
end
The do block executes after some seconds, warning me that it’s attempting to remove a probably state pidfile. I find it odd, since I just created it.
The most surprising thing is however that after the do block ends the file just disappears. I though I did nothing on it! What happened?
I dug into the code for mkpidlock and found out that a likely culprit is the tryrmopenfile called by open_exclusive, in FileWatching/src/pidfile.jl, but I don’t understand why the file should get deleted. And also the reason for the initial waiting period in the first place. What am I missing here? Is the function erasing the file I created because there was a stale pidfile?