Rev 6177: Change the TCPConnectionHandler to inherit from BaseRequestHandler. in http://bazaar.launchpad.net/~jameinel/bzr/2.5-no-hanging-teardown

John Arbash Meinel john at arbash-meinel.com
Mon Oct 3 07:07:11 UTC 2011


At http://bazaar.launchpad.net/~jameinel/bzr/2.5-no-hanging-teardown

------------------------------------------------------------
revno: 6177
revision-id: john at arbash-meinel.com-20111003070653-bbh7nkafs1r7ygjw
parent: john at arbash-meinel.com-20111003065619-5ov2sw2643rtjqdf
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.5-no-hanging-teardown
timestamp: Mon 2011-10-03 09:06:53 +0200
message:
  Change the TCPConnectionHandler to inherit from BaseRequestHandler.
  
  The problem with StreamRequestHandler is that it uses makefile, which causes the
  socket connection to have 2 handles, and just closing one isn't enough to
  actually force the connection closed.
  
  They are also known to interact poorly with stuff like blocking/nonblocking, etc.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_test_server.py'
--- a/bzrlib/tests/test_test_server.py	2011-10-03 06:56:19 +0000
+++ b/bzrlib/tests/test_test_server.py	2011-10-03 07:06:53 +0000
@@ -62,7 +62,7 @@
         return self.sock.recv(bufsize)
 
 
-class TCPConnectionHandler(SocketServer.StreamRequestHandler):
+class TCPConnectionHandler(SocketServer.BaseRequestHandler):
 
     def handle(self):
         self.done = False
@@ -70,12 +70,22 @@
         while not self.done:
             self.handle_connection()
 
+    def readline(self):
+        # TODO: We should be buffering any extra data sent, etc. However, in
+        #       practice, we don't send extra content, so we haven't bothered
+        #       to implement it yet.
+        req = self.request.recv(4096)
+        # An empty string is allowed, to indicate the end of the connection
+        if not req or (req.endswith('\n') and req.count('\n') == 1):
+            return req
+        raise ValueError('[%r] not a simple line' % (req,))
+
     def handle_connection(self):
-        req = self.rfile.readline()
+        req = self.readline()
         if not req:
             self.done = True
         elif req == 'ping\n':
-            self.wfile.write('pong\n')
+            self.request.sendall('pong\n')
         else:
             raise ValueError('[%s] not understood' % req)
 
@@ -149,7 +159,7 @@
         class CantConnect(Exception):
             pass
 
-        class FailingConnectionHandler(SocketServer.BaseRequestHandler):
+        class FailingConnectionHandler(TCPConnectionHandler):
 
             def handle(self):
                 raise CantConnect()
@@ -180,7 +190,7 @@
         class FailingDuringResponseHandler(TCPConnectionHandler):
 
             def handle_connection(self):
-                req = self.rfile.readline()
+                req = self.readline()
                 threading.currentThread().set_sync_event(sync)
                 raise FailToRespond()
 



More information about the bazaar-commits mailing list