Skip to content

Commit

Permalink
Fix #3052: Postpone web task creation to connection establishment
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jun 13, 2018
1 parent 36b27f9 commit 8735f91
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/3052.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Postpone web task creation to connection establishment.
13 changes: 8 additions & 5 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8735f91

Please sign in to comment.