changeset 30921:1f151a33af8e

lock: include Linux pid namespace identifier in prefix Previously, the lock only contains a hostname as an attempt to detect pid namespace difference. However, that's not enough on modern Linux - a single hostname could have different pid namespaces. That means if people run hg inside different PID namespaces with a same UTS namespae, the lock would be broken - an hg proccess in pid namespace A will think the lock having a "random" pid in pid namespace B is "dead" and remove it. This patch solves the above issue by appending an PID namespace identifier of the current process to the lock prefix ("hostname"). It depends on /proc being mounted properly. But I don't think there is a better way to get pid namespace identifier reliably.
author Jun Wu <quark@fb.com>
date Fri, 10 Feb 2017 13:56:31 -0800
parents dc9f086c7691
children 1beeb5185930
files mercurial/lock.py
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/lock.py	Fri Feb 10 13:35:21 2017 -0800
+++ b/mercurial/lock.py	Fri Feb 10 13:56:31 2017 -0800
@@ -9,12 +9,14 @@
 
 import contextlib
 import errno
+import os
 import socket
 import time
 import warnings
 
 from . import (
     error,
+    pycompat,
     util,
 )
 
@@ -22,9 +24,17 @@
     """Return a string which is used to differentiate pid namespaces
 
     It's useful to detect "dead" processes and remove stale locks with
-    confidence. Typically it's just hostname.
+    confidence. Typically it's just hostname. On modern linux, we include an
+    extra Linux-specific pid namespace identifier.
     """
-    return socket.gethostname()
+    result = socket.gethostname()
+    if pycompat.sysplatform.startswith('linux'):
+        try:
+            result += '/%x' % os.stat('/proc/self/ns/pid').st_ino
+        except OSError as ex:
+            if ex.errno not in (errno.ENOENT, errno.EACCES, errno.ENOTDIR):
+                raise
+    return result
 
 class lock(object):
     '''An advisory lock held by one process to control access to a set