Patchwork [1,of,2] lock: if we fork, ensure that only the parent releases

login
register
mail settings
Submitter Bryan O'Sullivan
Date April 11, 2013, 8:30 p.m.
Message ID <3d2ead77daf3f2ec8a49.1365712248@dev1718.prn1.facebook.com>
Download mbox | patch
Permalink /patch/1274/
State Accepted, archived
Delegated to: Augie Fackler
Headers show

Comments

Bryan O'Sullivan - April 11, 2013, 8:30 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1365712227 25200
#      Thu Apr 11 13:30:27 2013 -0700
# Node ID 3d2ead77daf3f2ec8a49fa4baf0cfb69e20fbaa6
# Parent  e798e7cc19d8658e40c3c25264213251f8e8b2fc
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.)

Patch

diff --git a/mercurial/lock.py b/mercurial/lock.py
--- a/mercurial/lock.py
+++ b/mercurial/lock.py
@@ -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: