[PATCH 1 of 7] commandserver: manually create file objects from socket

Yuya Nishihara yuya at tcha.org
Thu Jul 14 11:20:39 EDT 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1463888665 -32400
#      Sun May 22 12:44:25 2016 +0900
# Node ID 5944579fa6f60b65150bf9081264c199f4c86e65
# Parent  9901af3429a74276ac1611ca14da3a3661f5c38f
commandserver: manually create file objects from socket

Prepares for moving away from SocketServer. See the subsequent patches for
why.

diff --git a/hgext/chgserver.py b/hgext/chgserver.py
--- a/hgext/chgserver.py
+++ b/hgext/chgserver.py
@@ -533,10 +533,10 @@ class chgcmdserver(commandserver.server)
                          'setumask': setumask})
 
 class _requesthandler(commandserver._requesthandler):
-    def _createcmdserver(self):
+    def _createcmdserver(self, conn, fin, fout):
         ui = self.server.ui
         repo = self.server.repo
-        return chgcmdserver(ui, repo, self.rfile, self.wfile, self.connection,
+        return chgcmdserver(ui, repo, fin, fout, conn,
                             self.server.hashstate, self.server.baseaddress)
 
 def _tempaddress(address):
diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -338,7 +338,7 @@ class pipeservice(object):
             sv.cleanup()
             _restoreio(ui, fin, fout)
 
-class _requesthandler(socketserver.StreamRequestHandler):
+class _requesthandler(socketserver.BaseRequestHandler):
     def handle(self):
         # use a different process group from the master process, making this
         # process pass kernel "is_current_pgrp_orphaned" check so signals like
@@ -348,9 +348,13 @@ class _requesthandler(socketserver.Strea
         # same state inherited from parent.
         random.seed()
         ui = self.server.ui
+
+        conn = self.request
+        fin = conn.makefile('rb')
+        fout = conn.makefile('wb')
         sv = None
         try:
-            sv = self._createcmdserver()
+            sv = self._createcmdserver(conn, fin, fout)
             try:
                 sv.serve()
             # handle exceptions that may be raised by command server. most of
@@ -370,17 +374,23 @@ class _requesthandler(socketserver.Strea
             if sv:
                 cerr = sv.cerr
             else:
-                cerr = channeledoutput(self.wfile, 'e')
+                cerr = channeledoutput(fout, 'e')
             traceback.print_exc(file=cerr)
             raise
         finally:
+            fin.close()
+            try:
+                fout.close()  # implicit flush() may cause another EPIPE
+            except IOError as inst:
+                if inst.errno != errno.EPIPE:
+                    raise
             # trigger __del__ since ForkingMixIn uses os._exit
             gc.collect()
 
-    def _createcmdserver(self):
+    def _createcmdserver(self, conn, fin, fout):
         ui = self.server.ui
         repo = self.server.repo
-        return server(ui, repo, self.rfile, self.wfile)
+        return server(ui, repo, fin, fout)
 
 class unixservice(object):
     """


More information about the Mercurial-devel mailing list