lock: if we fork, ensure that only the parent releases
authorBryan O'Sullivan <bryano@fb.com>
Thu, 11 Apr 2013 13:30:27 -0700
changeset 18907 af9fa8d4c939
parent 18906 ef7068173a22
child 18908 779e3d9b7a1d
lock: if we fork, ensure that only the parent releases This prevents us from having a bunch of errant worker processes all try to release a lock if a problem occurs. (Releasing the lock more than once is harmless; it's invoking the associated callbacks we want to avoid.)
mercurial/lock.py
--- a/mercurial/lock.py	Thu Apr 11 14:44:22 2013 +0200
+++ b/mercurial/lock.py	Thu Apr 11 13:30:27 2013 -0700
@@ -36,6 +36,7 @@
         self.releasefn = releasefn
         self.desc = desc
         self.postrelease  = []
+        self.pid = os.getpid()
         self.lock()
 
     def __del__(self):
@@ -71,7 +72,7 @@
             return
         if lock._host is None:
             lock._host = socket.gethostname()
-        lockname = '%s:%s' % (lock._host, os.getpid())
+        lockname = '%s:%s' % (lock._host, self.pid)
         while not self.held:
             try:
                 util.makelock(lockname, self.f)
@@ -133,6 +134,9 @@
             self.held -= 1
         elif self.held == 1:
             self.held = 0
+            if os.getpid() != self.pid:
+                # we forked, and are not the parent
+                return
             if self.releasefn:
                 self.releasefn()
             try: