rubygitconcurrencyrugged

Are rugged Repository instances threadsafe?


My question boils down to the title: are rugged Repository instances (intended to be) threadsafe?

While I'm at it, I may be able to settle a question I've been having longer: is access to a git repository using rugged (intended to be) threadsafe when using different Repository instances?

Context

I'm using Rugged to access a git repository that stores documents for multiple users that can access the repo via a shared web frontend. So far I created a new Repository instance for each access, as that performs well enough and seems to be safe (I can't find guarantees in the docs or determine obvious safety from the way libgit2 is used, but no tests found problems and I'm assuming libgit2 itself is safe).

However, I ran into an issue, which limits the amount of repository instances that you can open near-simultaneously, which causes problems for some scripts that reuse some of the code that create Repository instances for each git repository access. An easy solution would be to share Repository instances between all users. However, that will cause problems if repository instances are not thread safe. Do I need to guard all of these shared instances with a Mutex or can I do without, because rugged/libgit2 already solves this problem for me?


Solution

  • Yes, libgit2 (and thus rugged as well) should be threadsafe, as long as you don't use the same repository instance (or any other object created from libgit2) across different threads.

    But as indicated by the second part of your question, you actually want to use the same repository instance across different threads. Here, the answe is it depends. Most, but not all, of the functions provided by libgit2 should be threadsafe, but I can't give you a definite list. See https://github.com/libgit2/libgit2/issues/2491 for a bit of more information.