Ray's Knowledge Base
Unlock

Calling Git2Backend::repo() twice in one scope self-deadlocks the repo mutex

PitfallVerified 19 Oct 2026Holds project: rgit
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A backend method hangs forever in tests: cargo test -p rgit-git --test it ... prints "has been running for over 60 seconds" and never finishes, with no error, no output and no CPU use.

Cause#

In rgit's crates/rgit-git/src/git_repo.rs, self.repo() returns a MutexGuard on a std Mutex that is not reentrant. Holding a guard from let repo = self.repo(); and then calling self.repo() again (e.g. to pass &mut self.repo() into rebase::replay) waits on the same lock forever, because the first guard lives to the end of the block.

Fix#

Read everything you need under one handle inside a block, return the plain values, and only then take the handle again:

let parent = {
    let repo = self.repo();
    // ... reads and checks ...
    parent.to_string()
};
crate::rebase::replay(&mut self.repo(), rev, Some(&parent), &|_| {})

Evidence#

In rgit, Git2Backend::drop_commit did let repo = self.repo(); ... crate::rebase::replay(&mut self.repo(), ...); two of its three integration tests hung until the 10-minute timeout. Scoping the first guard into a block made cargo test -p rgit-git --test it history::drop_commit pass in 0.3s.