[Python-checkins] bpo-27682: Handle client connection terminations in wsgiref (GH-9713)

Berker Peksag webhook-mailer at python.org
Wed May 1 13:32:23 EDT 2019


https://github.com/python/cpython/commit/3d37ea25dc97e4cb024045581979570835deb13c
commit: 3d37ea25dc97e4cb024045581979570835deb13c
branch: master
author: Petter Strandmark <petter.strandmark at gmail.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2019-05-01T20:32:15+03:00
summary:

bpo-27682: Handle client connection terminations in wsgiref (GH-9713)

files:
A Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst
M Lib/test/test_wsgiref.py
M Lib/wsgiref/handlers.py

diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
index 737dfed3a51e..46f88a94434b 100644
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -788,6 +788,24 @@ def flush(self):
             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()
diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py
index 28ed9b7a6d03..834073d50091 100644
--- a/Lib/wsgiref/handlers.py
+++ b/Lib/wsgiref/handlers.py
@@ -136,6 +136,10 @@ def run(self, application):
             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
index 000000000000..7b63c05ed2be
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst
@@ -0,0 +1,2 @@
+:class:`wsgiref.handlers.BaseHandler` now handles abrupt client connection
+terminations gracefully. Patch by Petter Strandmark.



More information about the Python-checkins mailing list