Commit 7c59362a authored by Berker Peksag's avatar Berker Peksag Committed by GitHub

bpo-29183: Fix double exceptions in wsgiref.handlers.BaseHandler (GH-12914)

parent f4e1babf
......@@ -806,6 +806,31 @@ class HandlerTests(TestCase):
self.assertFalse(stderr.getvalue())
def testDontResetInternalStateOnException(self):
class CustomException(ValueError):
pass
# We are raising CustomException here to trigger an exception
# during the execution of SimpleHandler.finish_response(), so
# we can easily test that the internal state of the handler is
# preserved in case of an exception.
class AbortingWriter:
def write(self, b):
raise CustomException
stderr = StringIO()
environ = {"SERVER_PROTOCOL": "HTTP/1.0"}
h = SimpleHandler(BytesIO(), AbortingWriter(), stderr, environ)
h.run(hello_app)
self.assertIn("CustomException", stderr.getvalue())
# Test that the internal state of the handler is preserved.
self.assertIsNotNone(h.result)
self.assertIsNotNone(h.headers)
self.assertIsNotNone(h.status)
self.assertIsNotNone(h.environ)
if __name__ == "__main__":
unittest.main()
......@@ -183,7 +183,16 @@ class BaseHandler:
for data in self.result:
self.write(data)
self.finish_content()
finally:
except:
# Call close() on the iterable returned by the WSGI application
# in case of an exception.
if hasattr(self.result, 'close'):
self.result.close()
raise
else:
# We only call close() when no exception is raised, because it
# will set status, result, headers, and environ fields to None.
# See bpo-29183 for more details.
self.close()
......
Fix double exceptions in :class:`wsgiref.handlers.BaseHandler` by calling
its :meth:`~wsgiref.handlers.BaseHandler.close` method only when no
exception is raised.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment