diff mercurial/util.py @ 1877:d314a89fa4f1

change lock format to let us detect and break stale locks. old style: symlink to pid new style: symlink to hostname:pid if lock code finds new-style lock, it breaks lock if locking pid is on same machine and pid is not alive. otherwise, lock is left alone. this makes locking code safe with old-style locks and with locks on other machines. new code makes server part of mercurial more robust in case machine crashes, power fails, or crazy user does kill -9.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Fri, 10 Mar 2006 08:31:31 -0800
parents bdfb524d728a
children 05c7d75be925
line wrap: on
line diff
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -499,7 +499,7 @@ if os.name == 'nt':
         return pf
 
     try: # ActivePython can create hard links using win32file module
-        import win32file
+        import win32api, win32con, win32file
 
         def os_link(src, dst): # NB will only succeed on NTFS
             win32file.CreateHardLink(dst, src)
@@ -516,8 +516,18 @@ if os.name == 'nt':
             except:
                 return os.stat(pathname).st_nlink
 
+        def testpid(pid):
+            '''return False if pid is dead, True if running or not known'''
+            try:
+                win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION,
+                                     False, pid)
+            except:
+                return True
+
     except ImportError:
-        pass
+        def testpid(pid):
+            '''return False if pid dead, True if running or not known'''
+            return True
 
     def is_exec(f, last):
         return last
@@ -614,6 +624,14 @@ else:
             else:
                 raise
 
+    def testpid(pid):
+        '''return False if pid dead, True if running or not sure'''
+        try:
+            os.kill(pid, 0)
+            return True
+        except OSError, inst:
+            return inst.errno != errno.ESRCH
+
     def explain_exit(code):
         """return a 2-tuple (desc, code) describing a process's status"""
         if os.WIFEXITED(code):