Skip to content

Commit f16d6f9

Browse files
committed
bpo-34547: Handle client connection terminations in wsgiref.
Previously, long stack traces with Python TypeErrors and AttributeErrors were printed (see bug). The wsgiref server is used quite a lot in the community, notably by the Django development server. I see these huge stack traces daily.
1 parent e9a044e commit f16d6f9

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/test/test_wsgiref.py

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

791+
def testConnectionAbortedError(self):
792+
class AbortingWriter:
793+
def write(self, b):
794+
raise ConnectionAbortedError()
795+
796+
def flush(self):
797+
pass
798+
799+
environ = {"SERVER_PROTOCOL": "HTTP/1.0"}
800+
h = SimpleHandler(BytesIO(), AbortingWriter(), sys.stderr, environ)
801+
h.run(hello_app)
802+
791803

792804
if __name__ == "__main__":
793805
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:
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``wsgiref`` now handles abrupt client connection terminations gracefully.
2+
Previously, long stack traces with Python TypeErrors and AttributeErrors
3+
were printed. Patch by Petter Strandmark.

0 commit comments

Comments
 (0)