diff --git a/hgext3rd/extutil.py b/hgext3rd/extutil.py --- a/hgext3rd/extutil.py +++ b/hgext3rd/extutil.py @@ -9,12 +9,10 @@ import contextlib import errno -import fcntl import os import subprocess import time -from mercurial.i18n import _ from mercurial import ( error, lock as lockmod, @@ -136,45 +134,6 @@ return cls return wrap -class fcntllock(object): - """A fcntllock based lock object. Currently it is always non-blocking. - - Note that since it is fcntllock based, you can accidentally take it multiple - times within one process and the first one to be released will release all - of them. So the caller needs to be careful to not create more than one - instance per lock. - """ - def __init__(self, opener, name, description): - self.path = opener.join(name) - self.description = description - self.fp = None - - def __enter__(self): - path = self.path - if self.fp is not None: - raise error.Abort(_("unable to re-enter lock '%s'") % path) - try: - self.fp = open(path, 'w+') - except (IOError, OSError) as ex: - raise error.Abort(_("unable to create lock file '%s': %s") % - (path, str(ex))) - - try: - fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError: - self.fp.close() - self.fp = None - raise error.LockHeld(errno.EAGAIN, path, self.description, '') - - return self - - def __exit__(self, type, value, traceback): - fp = self.fp - if fp is not None: - fcntl.lockf(fp, fcntl.LOCK_UN) - fp.close() - self.fp = None - @contextlib.contextmanager def flock(lockpath, description, timeout=-1): """A flock based lock object. Currently it is always non-blocking. diff --git a/remotefilelog/debugcommands.py b/remotefilelog/debugcommands.py --- a/remotefilelog/debugcommands.py +++ b/remotefilelog/debugcommands.py @@ -20,7 +20,7 @@ ) from .repack import repacklockvfs from .lz4wrapper import lz4decompress -import hashlib, os, time +import hashlib, os def debugremotefilelog(ui, path, **opts): decompress = opts.get('decompress') @@ -360,12 +360,8 @@ short(p2node), short(linknode), copyfrom)) def debugwaitonrepack(repo): - while True: - try: - with extutil.fcntllock(repacklockvfs(repo), 'repacklock', ''): - return - except error.LockHeld: - time.sleep(0.1) + with extutil.flock(repacklockvfs(repo).join('repacklock'), ''): + return def debugwaitonprefetch(repo): with repo._lock(repo.svfs, "prefetchlock", True, None, diff --git a/remotefilelog/repack.py b/remotefilelog/repack.py --- a/remotefilelog/repack.py +++ b/remotefilelog/repack.py @@ -1,7 +1,7 @@ from __future__ import absolute_import import os -from hgext3rd.extutil import runshellcommand, fcntllock +from hgext3rd.extutil import runshellcommand, flock from mercurial import ( error, extensions, @@ -440,8 +440,8 @@ def run(self, targetdata, targethistory): ledger = repackledger() - with fcntllock(repacklockvfs(self.repo), "repacklock", - _('repacking %s') % self.repo.origroot): + with flock(repacklockvfs(self.repo).join("repacklock"), + _('repacking %s') % self.repo.origroot, timeout=0): self.repo.hook('prerepack') # Populate ledger from source diff --git a/tests/test-extutil.py b/tests/test-extutil.py --- a/tests/test-extutil.py +++ b/tests/test-extutil.py @@ -52,12 +52,13 @@ except OSError as ex: self.assertEqual(ex.errno, errno.EACCES) - def testfcntllock(self): + def testflock(self): testtmp = os.environ["TESTTMP"] opener = vfs.vfs(testtmp) name = 'testlock' - with extutil.fcntllock(opener, name, 'testing a lock'): + with extutil.flock(opener.join(name), 'testing a lock', + timeout=0): otherlock = self.otherprocesslock(opener, name) self.assertEquals(otherlock, locktimeout, "other process should not have taken the lock") @@ -70,7 +71,8 @@ pid = os.fork() if pid == 0: try: - with extutil.fcntllock(opener, name, 'other process lock'): + with extutil.flock(opener.join(name), 'other process lock', + timeout=0): os._exit(locksuccess) except error.LockHeld: os._exit(locktimeout)