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

login
register
mail settings
Submitter Jun Wu
Date May 6, 2017, 11:39 p.m.
Message ID <9652453e198e525362e9.1494113954@x1c>
Download mbox | patch
Permalink /patch/20497/
State Accepted
Headers show

Comments

Jun Wu - May 6, 2017, 11:39 p.m.
# HG changeset patch
# User Jun Wu <quark@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.
Yuya Nishihara - May 9, 2017, 1:54 p.m.
On Sat, 6 May 2017 16:39:14 -0700, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@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

Queued 1-2, thanks.

> 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)

Added "import socket" in flight.

Patch

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