Rev 6165: Don't use server.socket.close() go through the server._close() abstraction. in http://bazaar.launchpad.net/~jameinel/bzr/drop-idle-connections-824797

John Arbash Meinel john at arbash-meinel.com
Thu Sep 15 13:58:37 UTC 2011


At http://bazaar.launchpad.net/~jameinel/bzr/drop-idle-connections-824797

------------------------------------------------------------
revno: 6165
revision-id: john at arbash-meinel.com-20110915135823-45lcddaf5f71hbhm
parent: john at arbash-meinel.com-20110915131003-vhca56irzrjifkmh
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: drop-idle-connections-824797
timestamp: Thu 2011-09-15 15:58:23 +0200
message:
  Don't use server.socket.close() go through the server._close() abstraction.
-------------- next part --------------
=== modified file 'bzrlib/smart/medium.py'
--- a/bzrlib/smart/medium.py	2011-09-15 12:20:38 +0000
+++ b/bzrlib/smart/medium.py	2011-09-15 13:58:23 +0000
@@ -285,24 +285,20 @@
         """select() on a file descriptor, waiting for nonblocking read()"""
         # Use local variables to handle when the interpreter is shutting down
         try:
-            if (timeout_seconds is None
-                or timeout_seconds <= self._client_poll_timeout):
-                rs, _, _ = select.select([fd], [], [], timeout_seconds)
-            else:
-                # It looks like during the test suite, we close the server-side
-                # socket as part of 'shut down this server'. Depending on how
-                # we race with select.select, that either
-                # 1) Raises socket.error(EBADF) immediately
-                # 2) Occasionally (1 in 1000 or so) raises select.error(EBADF)
-                # 3) 1-in-3 or so times out, calling select.select immediately
-                #    afterwards seems to raise EBADF.
-                # I think what happens is select.select is unable to see the
-                # status of a file that is closed after it starts 'sleeping'.
-                t_end = time.time() + timeout_seconds
-                rs = []
-                while not rs and time.time() < t_end:
-                    rs, _, _ = select.select([fd], [], [],
-                                             self._client_poll_timeout)
+            # It looks like during the test suite, we close the server-side
+            # socket as part of 'shut down this server'. Depending on how
+            # we race with select.select, that either
+            # 1) Raises socket.error(EBADF) immediately
+            # 2) Occasionally (1 in 1000 or so) raises select.error(EBADF)
+            # 3) 1-in-3 or so times out, calling select.select immediately
+            #    afterwards seems to raise EBADF.
+            # I think what happens is select.select is unable to see the
+            # status of a file that is closed after it starts 'sleeping'.
+            t_end = time.time() + timeout_seconds
+            poll_timeout = min(timeout_seconds, self._client_poll_timeout)
+            rs = []
+            while not rs and time.time() < t_end:
+                rs, _, _ = select.select([fd], [], [], poll_timeout)
         except (select.error, socket.error) as e:
             err = getattr(e, 'errno', None)
             if err is None:

=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py	2011-09-15 12:20:38 +0000
+++ b/bzrlib/tests/test_server.py	2011-09-15 13:58:23 +0000
@@ -265,7 +265,7 @@
             #raise AssertionError('thread %s hung' % (self.name,))
 
 
-class TestingTCPServerMixin:
+class TestingTCPServerMixin(object):
     """Mixin to support running SocketServer.TCPServer in a thread.
 
     Tests are connecting from the main thread, the server has to be run in a

=== modified file 'bzrlib/tests/test_smart_transport.py'
--- a/bzrlib/tests/test_smart_transport.py	2011-09-15 12:41:45 +0000
+++ b/bzrlib/tests/test_smart_transport.py	2011-09-15 13:58:23 +0000
@@ -727,7 +727,7 @@
         sample_protocol = SampleRequest(expected_bytes=sample_request_bytes)
         client_sock.sendall(sample_request_bytes)
         server._serve_one_request(sample_protocol)
-        server.socket.close()
+        server._close()
         self.assertEqual('', client_sock.recv(1))
         self.assertEqual(sample_request_bytes, sample_protocol.accepted_bytes)
         self.assertFalse(server.finished)
@@ -761,7 +761,7 @@
         server_protocol = server._build_protocol()
         client_sock.sendall(rest_of_request_bytes)
         server._serve_one_request(server_protocol)
-        server.socket.close()
+        server._close()
         self.assertEqual(expected_response, osutils.recv_all(client_sock, 50),
                          "Not a version 2 response to 'hello' request.")
         self.assertEqual('', client_sock.recv(1))
@@ -840,7 +840,7 @@
         stream_still_open = server._serve_one_request(second_protocol)
         self.assertEqual(sample_request_bytes, second_protocol.accepted_bytes)
         self.assertFalse(server.finished)
-        server.socket.close()
+        server._close()
         self.assertEqual('', client_sock.recv(1))
 
     def test_pipe_like_stream_error_handling(self):
@@ -882,7 +882,7 @@
         fake_protocol = ErrorRaisingProtocol(KeyboardInterrupt('boom'))
         self.assertRaises(
             KeyboardInterrupt, server._serve_one_request, fake_protocol)
-        server.socket.close()
+        server._close()
         self.assertEqual('', client_sock.recv(1))
 
     def build_protocol_pipe_like(self, bytes):



More information about the bazaar-commits mailing list