diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -683,8 +683,10 @@ class localrepository(repo.repository): return l def lock(self, wait=True): - if self._lockref and self._lockref(): - return self._lockref() + l = self._lockref and self._lockref() + if l is not None and l.held: + l.lock() + return l l = self._lock(self.sjoin("lock"), wait, None, self.invalidate, _('repository %s') % self.origroot) @@ -692,8 +694,10 @@ class localrepository(repo.repository): return l def wlock(self, wait=True): - if self._wlockref and self._wlockref(): - return self._wlockref() + l = self._wlockref and self._wlockref() + if l is not None and l.held: + l.lock() + return l l = self._lock(self.join("wlock"), wait, self.dirstate.write, self.dirstate.invalidate, _('working directory of %s') % diff --git a/mercurial/lock.py b/mercurial/lock.py --- a/mercurial/lock.py +++ b/mercurial/lock.py @@ -27,6 +27,11 @@ class lock(object): self.lock() def __del__(self): + if self.held: + # ensure the lock will be removed + # even if recursive locking did occur + self.held = 1 + self.release() def lock(self): @@ -45,6 +50,9 @@ class lock(object): inst.locker) def trylock(self): + if self.held: + self.held += 1 + return if lock._host is None: lock._host = socket.gethostname() lockname = '%s:%s' % (lock._host, os.getpid()) @@ -97,7 +105,9 @@ class lock(object): return locker def release(self): - if self.held: + if self.held > 1: + self.held -= 1 + elif self.held is 1: self.held = 0 if self.releasefn: self.releasefn() @@ -105,3 +115,8 @@ class lock(object): os.unlink(self.f) except: pass +def release(*locks): + for lock in locks: + if lock is not None: + lock.release() +