diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index 2621e92399..66487502d5 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -289,6 +289,9 @@ class ProxyLoggingMiddleware(object): self.log_request( req, status_int, input_proxy.bytes_received, bytes_sent, start_time, time.time(), resp_headers=resp_headers) + close_method = getattr(iterable, 'close', None) + if callable(close_method): + close_method() try: iterable = self.app(env, my_start_response) diff --git a/test/unit/common/middleware/test_proxy_logging.py b/test/unit/common/middleware/test_proxy_logging.py index 47147b9915..6f1fc9f5fa 100644 --- a/test/unit/common/middleware/test_proxy_logging.py +++ b/test/unit/common/middleware/test_proxy_logging.py @@ -430,6 +430,27 @@ class TestProxyLogging(unittest.TestCase): self.assertEquals(log_parts[0], '1.2.3.4') # client ip self.assertEquals(log_parts[1], '1.2.3.4') # remote addr + def test_iterator_closing(self): + + class CloseableBody(object): + def __init__(self): + self.closed = False + + def close(self): + self.closed = True + + def __iter__(self): + return iter(["CloseableBody"]) + + body = CloseableBody() + app = proxy_logging.ProxyLoggingMiddleware(FakeApp(body), {}) + req = Request.blank('/', environ={'REQUEST_METHOD': 'GET', + 'REMOTE_ADDR': '1.2.3.4'}) + resp = app(req.environ, start_response) + # exhaust generator + [x for x in resp] + self.assertTrue(body.closed) + def test_proxy_client_logging(self): app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {}) app.access_logger = FakeLogger()