[Python-checkins] cpython (2.7): Issue #26309: Shut down SocketServer request if verify_request() is false

martin.panter python-checkins at python.org
Thu Feb 18 06:54:35 EST 2016


https://hg.python.org/cpython/rev/e0fbd25f0b36
changeset:   100259:e0fbd25f0b36
branch:      2.7
parent:      100254:c272a1e53f5b
user:        Martin Panter <vadmium+py at gmail.com>
date:        Thu Feb 18 10:43:55 2016 +0000
summary:
  Issue #26309: Shut down SocketServer request if verify_request() is false

Based on patch by Aviv Palivoda.

files:
  Lib/SocketServer.py           |   2 ++
  Lib/test/test_socketserver.py |  23 +++++++++++++++++++++++
  Misc/NEWS                     |   4 ++++
  3 files changed, 29 insertions(+), 0 deletions(-)


diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py
--- a/Lib/SocketServer.py
+++ b/Lib/SocketServer.py
@@ -296,6 +296,8 @@
             except:
                 self.handle_error(request, client_address)
                 self.shutdown_request(request)
+        else:
+            self.shutdown_request(request)
 
     def handle_timeout(self):
         """Called if no new request arrives within self.timeout.
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -326,6 +326,29 @@
                 SocketServer.TCPServer((HOST, -1),
                                        SocketServer.StreamRequestHandler)
 
+    def test_shutdown_request_called_if_verify_request_false(self):
+        # Issue #26309: BaseServer should call shutdown_request even if
+        # verify_request is False
+        result = {"shutdown_called": False}
+
+        class MyServer(SocketServer.TCPServer):
+            def verify_request(self, request, client_address):
+                return False
+
+            def shutdown_request(self, request):
+                result["shutdown_called"] = True
+                SocketServer.TCPServer.shutdown_request(self, request)
+
+        def connect_to_server(proto, addr):
+            s = socket.socket(proto, socket.SOCK_STREAM)
+            s.connect(addr)
+            s.close()
+
+        self.run_server(MyServer,
+                        SocketServer.StreamRequestHandler,
+                        connect_to_server)
+        self.assertEqual(result["shutdown_called"], True)
+
 
 def test_main():
     if imp.lock_held():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,10 @@
 Library
 -------
 
+- Issue #26309: In the "socketserver" module, shut down the request (closing
+  the connected socket) when verify_request() returns false.  Based on patch
+  by Aviv Palivoda.
+
 - Issue #25939: On Windows open the cert store readonly in ssl.enum_certificates.
 
 - Issue #24303: Fix random EEXIST upon multiprocessing semaphores creation with

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list