From 47ffc1a9f6fab1c17cdcc325d4af066317369ed7 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 1 May 2019 10:52:40 -0700 Subject: [PATCH] bpo-27682: Handle client connection terminations in wsgiref (GH-9713) (cherry picked from commit 3d37ea25dc97e4cb024045581979570835deb13c) Co-authored-by: Petter Strandmark --- Lib/test/test_wsgiref.py | 18 ++++++++++++++++++ Lib/wsgiref/handlers.py | 4 ++++ .../2018-10-05-16-01-00.bpo-34547.abbaa.rst | 2 ++ 3 files changed, 24 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-10-05-16-01-00.bpo-34547.abbaa.rst diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py index 8422b308d79..5502ece576f 100644 --- a/Lib/test/test_wsgiref.py +++ b/Lib/test/test_wsgiref.py @@ -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() diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index f4300b831a4..f04cef9b9d0 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -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 index 00000000000..7b63c05ed2b --- /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.