Skip to content

Commit 3d37ea2

Browse files
PetterSberkerpeksag
authored andcommitted
bpo-27682: Handle client connection terminations in wsgiref (GH-9713)
1 parent 18029d8 commit 3d37ea2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/test_wsgiref.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,24 @@ def flush(self):
788788
b"Hello, world!",
789789
written)
790790

791+
def testClientConnectionTerminations(self):
792+
environ = {"SERVER_PROTOCOL": "HTTP/1.0"}
793+
for exception in (
794+
ConnectionAbortedError,
795+
BrokenPipeError,
796+
ConnectionResetError,
797+
):
798+
with self.subTest(exception=exception):
799+
class AbortingWriter:
800+
def write(self, b):
801+
raise exception
802+
803+
stderr = StringIO()
804+
h = SimpleHandler(BytesIO(), AbortingWriter(), stderr, environ)
805+
h.run(hello_app)
806+
807+
self.assertFalse(stderr.getvalue())
808+
791809

792810
if __name__ == "__main__":
793811
unittest.main()

Lib/wsgiref/handlers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def run(self, application):
136136
self.setup_environ()
137137
self.result = application(self.environ, self.start_response)
138138
self.finish_response()
139+
except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError):
140+
# We expect the client to close the connection abruptly from time
141+
# to time.
142+
return
139143
except:
140144
try:
141145
self.handle_error()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`wsgiref.handlers.BaseHandler` now handles abrupt client connection
2+
terminations gracefully. Patch by Petter Strandmark.

0 commit comments

Comments
 (0)