diff --git a/CHANGES/3052.bugfix b/CHANGES/3052.bugfix new file mode 100644 index 00000000000..e2282561c43 --- /dev/null +++ b/CHANGES/3052.bugfix @@ -0,0 +1 @@ +Postpone web task creation to connection establishment. \ No newline at end of file diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index ecd1414a6ae..6c0c454d9ad 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -109,7 +109,7 @@ def __init__(self, manager, *, loop=None, self._waiter = None self._error_handler = None - self._task_handler = self._loop.create_task(self.start()) + self._task_handler = None self._upgrade = False self._payload_parser = None @@ -158,14 +158,16 @@ async def shutdown(self, timeout=15.0): # wait for handlers with suppress(asyncio.CancelledError, asyncio.TimeoutError): with CeilTimeout(timeout, loop=self._loop): - if self._error_handler and not self._error_handler.done(): + if (self._error_handler is not None and + not self._error_handler.done()): await self._error_handler - if self._task_handler and not self._task_handler.done(): + if (self._task_handler is not None and + not self._task_handler.done()): await self._task_handler # force-close non-idle handler - if self._task_handler: + if self._task_handler is not None: self._task_handler.cancel() if self.transport is not None: @@ -180,6 +182,7 @@ def connection_made(self, transport): tcp_cork(transport, False) tcp_nodelay(transport, True) + self._task_handler = self._loop.create_task(self.start()) self._manager.connection_made(self, transport) def connection_lost(self, exc): @@ -196,7 +199,7 @@ def connection_lost(self, exc): if self._keepalive_handle is not None: self._keepalive_handle.cancel() - if self._task_handler: + if self._task_handler is not None: self._task_handler.cancel() if self._error_handler is not None: diff --git a/tests/test_web_protocol.py b/tests/test_web_protocol.py index def68e64125..bd721c529f4 100644 --- a/tests/test_web_protocol.py +++ b/tests/test_web_protocol.py @@ -647,6 +647,7 @@ def test_rudimentary_transport(srv, loop): async def test_close(srv, loop, transport): transport.close.side_effect = partial(srv.connection_lost, None) srv.connection_made(transport) + await asyncio.sleep(0, loop=loop) srv.handle_request = mock.Mock() srv.handle_request.side_effect = helpers.noop