]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-27682: Handle client connection terminations in wsgiref (GH-9713)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 1 May 2019 17:52:40 +0000 (10:52 -0700)
committerBerker Peksag <berker.peksag@gmail.com>
Wed, 1 May 2019 17:52:40 +0000 (20:52 +0300)
(cherry picked from commit 3d37ea25dc97e4cb024045581979570835deb13c)

Co-authored-by: Petter Strandmark <petter.strandmark@gmail.com>
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 8422b308d795e9bfdec76b897052f62fc6796c29..5502ece576f477f266e9d2816b25295f01dc9cc4 100644 (file)
@@ -780,6 +780,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 f4300b831a44e40e41966d1d5c578566b4bb805e..f04cef9b9d06775d38d454b3baac8946740238c2 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.