From 772eca0b0219f63129282c3596fc210951433c13 Mon Sep 17 00:00:00 2001 From: Petter Strandmark Date: Thu, 3 Dec 2020 08:46:03 +0100 Subject: [PATCH] Fixed #32240 -- Made runserver suppress ConnectionAbortedError/ConnectionResetError errors. See https://bugs.python.org/issue27682 and https://github.com/python/cpython/pull/9713 --- AUTHORS | 1 + django/core/servers/basehttp.py | 6 +++++- tests/servers/test_basehttp.py | 31 +++++++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3dd968351b..b4b1fc2529 100644 --- a/AUTHORS +++ b/AUTHORS @@ -736,6 +736,7 @@ answer newbie questions, and generally made Django that much better: Peter Zsoldos Pete Shinners Petr Marhoun + Petter Strandmark pgross@thoughtworks.com phaedo phil.h.smith@gmail.com diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index 02957c51a2..c5b020e4f6 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -52,7 +52,11 @@ def get_internal_wsgi_application(): def is_broken_pipe_error(): exc_type, _, _ = sys.exc_info() - return issubclass(exc_type, BrokenPipeError) + return issubclass(exc_type, ( + BrokenPipeError, + ConnectionAbortedError, + ConnectionResetError, + )) class WSGIServer(simple_server.WSGIServer): diff --git a/tests/servers/test_basehttp.py b/tests/servers/test_basehttp.py index 4c685b282f..b90e8565a6 100644 --- a/tests/servers/test_basehttp.py +++ b/tests/servers/test_basehttp.py @@ -113,15 +113,22 @@ class WSGIServerTestCase(SimpleTestCase): request = WSGIRequest(self.request_factory.get('/').environ) client_address = ('192.168.2.0', 8080) msg = f'- Broken pipe from {client_address}\n' - try: - server = WSGIServer(('localhost', 0), WSGIRequestHandler) - try: - raise BrokenPipeError() - except Exception: - with captured_stderr() as err: - with self.assertLogs('django.server', 'INFO') as cm: - server.handle_error(request, client_address) - self.assertEqual(err.getvalue(), '') - self.assertEqual(cm.records[0].getMessage(), msg) - finally: - server.server_close() + tests = [ + BrokenPipeError, + ConnectionAbortedError, + ConnectionResetError, + ] + for exception in tests: + with self.subTest(exception=exception): + try: + server = WSGIServer(('localhost', 0), WSGIRequestHandler) + try: + raise exception() + except Exception: + with captured_stderr() as err: + with self.assertLogs('django.server', 'INFO') as cm: + server.handle_error(request, client_address) + self.assertEqual(err.getvalue(), '') + self.assertEqual(cm.records[0].getMessage(), msg) + finally: + server.server_close()