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

(cherry picked from commit 3d37ea25dc)

Co-authored-by: Petter Strandmark <petter.strandmark@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-05-01 10:52:40 -07:00 committed by Berker Peksag
parent 5f5b187bfa
commit 47ffc1a9f6
3 changed files with 24 additions and 0 deletions

View 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()

View 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()

View File

@ -0,0 +1,2 @@
:class:`wsgiref.handlers.BaseHandler` now handles abrupt client connection
terminations gracefully. Patch by Petter Strandmark.