From a6fe3ed5ccbc487fcd87954e22ebb69d70bf8d3c Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Wed, 12 Nov 2014 11:14:48 +0200 Subject: [PATCH] Fix #172: Set server.transport to None on .closing() --- CHANGES.txt | 2 ++ aiohttp/server.py | 1 + tests/test_http_server.py | 31 +++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index eb3c74823cb..b37b9d87eac 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -16,6 +16,8 @@ CHANGES - Drop closed connections from connector #173 +- Set server.transport to None on .closing() #172 + 0.9.3 (10-30-2014) ------------------ diff --git a/aiohttp/server.py b/aiohttp/server.py index 45272caf4b8..6560bdfa44f 100644 --- a/aiohttp/server.py +++ b/aiohttp/server.py @@ -102,6 +102,7 @@ def closing(self): if ((not self._reading_request or self._request_handler is None) and self.transport is not None): self.transport.close() + self.transport = None def connection_made(self, transport): super().connection_made(transport) diff --git a/tests/test_http_server.py b/tests/test_http_server.py index 52881228dd0..03b628e8cc3 100644 --- a/tests/test_http_server.py +++ b/tests/test_http_server.py @@ -48,11 +48,38 @@ def test_closing(self): srv._keep_alive_handle = keep_alive_handle timeout_handle = unittest.mock.Mock() srv._timeout_handle = timeout_handle - srv.transport = unittest.mock.Mock() + transport = srv.transport = unittest.mock.Mock() srv.writer = unittest.mock.Mock() srv.closing() - self.assertTrue(srv.transport.close.called) + self.assertTrue(transport.close.called) + self.assertIsNone(srv.transport) + + self.assertIsNotNone(srv._keep_alive_handle) + self.assertFalse(keep_alive_handle.cancel.called) + + self.assertIsNotNone(srv._timeout_handle) + self.assertFalse(timeout_handle.cancel.called) + + def test_double_closing(self): + srv = server.ServerHttpProtocol(loop=self.loop) + srv._keep_alive = True + + keep_alive_handle = unittest.mock.Mock() + srv._keep_alive_handle = keep_alive_handle + timeout_handle = unittest.mock.Mock() + srv._timeout_handle = timeout_handle + transport = srv.transport = unittest.mock.Mock() + srv.writer = unittest.mock.Mock() + + srv.closing() + self.assertTrue(transport.close.called) + self.assertIsNone(srv.transport) + + transport.reset_mock() + srv.closing() + self.assertFalse(transport.close.called) + self.assertIsNone(srv.transport) self.assertIsNotNone(srv._keep_alive_handle) self.assertFalse(keep_alive_handle.cancel.called)