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

**URL:** <https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161>\
**Category:** General Usage\
**Created:** [January 23, 2024, 4:52pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161 "2024-01-23T16:52:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 23, 2024, 4:52pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/1 "2024-01-23T16:52:52Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [January 23, 2024, 6:51pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/2 "2024-01-23T18:51:52Z")

</div>

Probably [File Events · The Julia Language](https://docs.julialang.org/en/v1/stdlib/FileWatching/#Pidfile)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 23, 2024, 7:08pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/3 "2024-01-23T19:08:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 23, 2024, 8:10pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/4 "2024-01-23T20:10:23Z")

</div>

This sounds awesome!

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

```julia
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 

```

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 23, 2024, 8:12pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/5 "2024-01-23T20:12:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 23, 2024, 8:32pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/6 "2024-01-23T20:32:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 23, 2024, 9:55pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/7 "2024-01-23T21:55:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 23, 2024, 9:58pm UTC](https://discourse.julialang.org/t/which-lock-to-use-when-locking-external-stateful-commands-like-git-or-file-systems/109161/8 "2024-01-23T21:58:10Z")

</div>

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.
