The server in the wsgiref module is actually used a lot. For example, it is the server Django uses for development.
A very common thing that happens during Django development is that the web browser closes the connection for some reason. Then very long stack traces appear in the Django logs:
[30/Aug/2018 12:10:38] "POST /login/ HTTP/1.1" 200 3964
Traceback (most recent call last):
File "d:\python37\Lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "d:\python37\Lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "d:\python37\Lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "d:\python37\Lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "d:\python37\Lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "d:\python37\Lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "d:\python37\Lib\socketserver.py", line 796, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
[30/Aug/2018 12:10:38] "POST /login/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50112)
Traceback (most recent call last):
File "d:\python37\Lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "d:\python37\Lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "d:\python37\Lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "d:\python37\Lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "d:\python37\Lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "d:\python37\Lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "d:\python37\Lib\socketserver.py", line 796, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\python37\Lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "D:\Virtualenvs\ledev-X6wd5Q8f\lib\site-packages\django\core\servers\basehttp.py", line 86, in handle_error
super().handle_error()
File "d:\python37\Lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "d:\python37\Lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "d:\python37\Lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "d:\python37\Lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "d:\python37\Lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\python37\Lib\socketserver.py", line 647, in process_request_thread
self.finish_request(request, client_address)
File "d:\python37\Lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "d:\python37\Lib\socketserver.py", line 717, in __init__
self.handle()
File "D:\Virtualenvs\ledev-X6wd5Q8f\lib\site-packages\django\core\servers\basehttp.py", line 154, in handle
handler.run(self.server.get_app())
File "d:\python37\Lib\wsgiref\handlers.py", line 144, in run
self.close()
File "d:\python37\Lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
Obviously, this is a bit annoying when developing with Django daily. Since Django simply uses the wsgiref server, I though the best solution was to handle closed connections more gracefully there.
I think the best solution is to simply add another except clause here: https://github.com/python/cpython/blob/e6dac0077996b1e1f886f036d6f2606237fa4c85/Lib/wsgiref/handlers.py#L142 . We should catch ConnectionAbortedError here and return from the function.
I am happy to create a PR if this is what we want to do.
|