]> granicus.if.org Git - python/commitdiff
bpo-27682: Handle client connection terminations in wsgiref (GH-9713)
authorPetter Strandmark <petter.strandmark@gmail.com>
Wed, 1 May 2019 17:32:15 +0000 (19:32 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Wed, 1 May 2019 17:32:15 +0000 (20:32 +0300)
Lib/test/test_wsgiref.py
Lib/wsgiref/handlers.py
Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst [new file with mode: 0644]

index 737dfed3a51e574ebb91cf28e1bf0821ffab46d0..46f88a94434bcdf0c42ae445d7852891accd3bbe 100644 (file)
@@ -788,6 +788,24 @@ class HandlerTests(TestCase):
             b"Hello, world!",
             written)
 
+    def testClientConnectionTerminations(self):
+        environ = {"SERVER_PROTOCOL": "HTTP/1.0"}
+        for exception in (
+            ConnectionAbortedError,
+            BrokenPipeError,
+            ConnectionResetError,
+        ):
+            with self.subTest(exception=exception):
+                class AbortingWriter:
+                    def write(self, b):
+                        raise exception
+
+                stderr = StringIO()
+                h = SimpleHandler(BytesIO(), AbortingWriter(), stderr, environ)
+                h.run(hello_app)
+
+                self.assertFalse(stderr.getvalue())
+
 
 if __name__ == "__main__":
     unittest.main()
index 28ed9b7a6d0353db063ab8802083d78e329a18ef..834073d50091e033ab232e95bfdd2394f9502fbb 100644 (file)
@@ -136,6 +136,10 @@ class BaseHandler:
             self.setup_environ()
             self.result = application(self.environ, self.start_response)
             self.finish_response()
+        except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError):
+            # We expect the client to close the connection abruptly from time
+            # to time.
+            return
         except:
             try:
                 self.handle_error()
diff --git a/Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst b/Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst
new file mode 100644 (file)
index 0000000..7b63c05
--- /dev/null
@@ -0,0 +1,2 @@
+:class:`wsgiref.handlers.BaseHandler` now handles abrupt client connection
+terminations gracefully. Patch by Petter Strandmark.