[PATCH 1 of 4 stateful-chg] commandserver: move "listen" responsibility from service to handler

Jun Wu quark at fb.com
Sat May 6 23:39:14 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1493575707 25200
#      Sun Apr 30 11:08:27 2017 -0700
# Node ID 9652453e198e525362e93c84712fb29c3297cf13
# Parent  31f42e683321f225eb9c306f8d4b3a9e8d82a1da
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 9652453e198e
commandserver: move "listen" responsibility from service to handler

This enables chg to replace a server socket in an atomic way:

  1. bind to a temp address
  2. listen
  3. rename

Currently 3 happens before 2 so a client may see the socket file but fails
to connect to it.

diff --git a/mercurial/chgserver.py b/mercurial/chgserver.py
--- a/mercurial/chgserver.py
+++ b/mercurial/chgserver.py
@@ -518,4 +518,5 @@ class chgunixservicehandler(object):
         util.bindunixsocket(sock, tempaddress)
         self._socketstat = os.stat(tempaddress)
+        sock.listen(socket.SOMAXCONN)
         # rename will replace the old socket file if exists atomically. the
         # old server will detect ownership change and exit.
diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -410,4 +410,5 @@ class unixservicehandler(object):
     def bindsocket(self, sock, address):
         util.bindunixsocket(sock, address)
+        sock.listen(socket.SOMAXCONN)
 
     def unlinksocket(self, address):
@@ -453,5 +454,4 @@ class unixforkingservice(object):
         self._sock = socket.socket(socket.AF_UNIX)
         self._servicehandler.bindsocket(self._sock, self.address)
-        self._sock.listen(socket.SOMAXCONN)
         o = signal.signal(signal.SIGCHLD, self._sigchldhandler)
         self._oldsigchldhandler = o


More information about the Mercurial-devel mailing list