# HG changeset patch # User Benoit Boissinot # Date 2006-02-21 22:21:15 # Node ID e431344e604c986d95314ba7ce35d8f0e2a7a066 # Parent b9671b41e360dfa04ea32d0e401dc6249d993438 add a timeout when a lock is held (default 1024 sec) - change the wait keyword from lock.lock to timeout, a negative timeout of means "wait forever" - refactor the two lock functions from localrepo.py - make them use the timeout (default 1024, can be changed with ui.timeout in the config file - update the doc diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt --- a/doc/hgrc.5.txt +++ b/doc/hgrc.5.txt @@ -247,6 +247,9 @@ ui:: remote command to use for clone/push/pull operations. Default is 'hg'. ssh;; command to use for SSH connections. Default is 'ssh'. + timeout;; + The timeout used when a lock is held (in seconds), a negative value + means no timeout. Default is 1024. username;; The committer of a changeset created when running "commit". Typically a person's name and email address, e.g. "Fred Widget diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -261,7 +261,14 @@ class localrepository(object): if not wait: raise inst self.ui.warn(_("waiting for lock held by %s\n") % inst.args[0]) - l = lock.lock(self.join(lockname), wait, releasefn) + try: + # default to 1024 seconds timeout + l = lock.lock(self.join(lockname), + int(self.ui.config("ui", "timeout") or 1024), + releasefn) + except lock.LockHeld, inst: + raise util.Abort(_("timeout while waiting for " + "lock held by %s") % inst.args[0]) if acquirefn: acquirefn() return l diff --git a/mercurial/lock.py b/mercurial/lock.py --- a/mercurial/lock.py +++ b/mercurial/lock.py @@ -16,10 +16,10 @@ class LockUnavailable(LockException): pass class lock(object): - def __init__(self, file, wait=1, releasefn=None): + def __init__(self, file, timeout=-1, releasefn=None): self.f = file self.held = 0 - self.wait = wait + self.timeout = timeout self.releasefn = releasefn self.lock() @@ -27,13 +27,16 @@ class lock(object): self.release() def lock(self): + timeout = self.timeout while 1: try: self.trylock() return 1 except LockHeld, inst: - if self.wait: + if timeout != 0: time.sleep(1) + if timeout > 0: + timeout -= 1 continue raise inst