Which Lock to use when locking external stateful commands like git or file systems?

Hi, I am wondering whether there is a standard Lock object I can use to secure something like

git add -A
git commit -m "this should be run together"

Probably File Events · The Julia Language

Any threadsafe lock on the Julia side will probably suffice to protect concurrent accesses to git from your program, the question is just if that is sufficient for your usecase. Do you also want to protect against other processes invoking git at the same time?

1 Like

This sounds awesome!

Unforunately it does not feature an example. I guess one uses mkpidlock like one would use lock, i.e.

mkpidlock("lock_filename") do  # this aquires the file as a lock, or waits for it to be aquirable
# ...
# do things which need to be locked on a system level
# ...
end  # will release the file again 

The problem here is that the standard lock seems to be a ReentrantLock, but here I was unsure, whether it is enough to secure git so that other task-switches cannot interfere at all…

I am unsure about how far the “reentrant” part goes and whether it interferes with my intention

As far as task switches go, ReentrantLock is the correct choice, if you want to protect against other tasks entering that same critical section of code.

Note that neither approach will prevent git operations on that repository entirely - other processes can spawn git at the same time and completely ignore your (process local or even pid) lock. The same goes for other tasks that aren’t managed by you and don’t go through your critical section.

What do you want to protect against?

2 Likes

Mainly task switches as I am on a julia process - but I appreciate the mkpidlock for being extendable to a fleet of processes some of which may not be julia

I’m not sure if it’s documented anywhere but git itself uses a lock file called index.lock. If you kill a git operation midway sometimes the lock file will still be there and you’ll have to delete it manually.

1 Like