Skip to content

Commit

Permalink
Fix #2773: InvalidStateError on two data_received() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Feb 27, 2018
1 parent 5d5e9fd commit 49a540b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Changelog

.. towncrier release notes start
3.0.5 (2018-02-27)
==================

- Fix ``InvalidStateError`` on processing a seria of two consequenced
``RequestHandler.data_received`` calls on web server. (#2773)

3.0.4 (2018-02-26)
==================

Expand Down
7 changes: 5 additions & 2 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ def data_received(self, data):
self._request_count += 1
self._messages.append((msg, payload))

if self._waiter is not None:
self._waiter.set_result(None)
waiter = self._waiter
if waiter is not None:
if not waiter.done():
# don't set result twice
waiter.set_result(None)

self._upgraded = upgraded
if upgraded and tail:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,23 @@ def test__process_keepalive_force_close(loop, srv):
with mock.patch.object(loop, "call_at") as call_at_patched:
srv._process_keepalive()
assert not call_at_patched.called


def test_two_data_received_without_waking_up_start_task(srv, loop):
# make a chance to srv.start() method start waiting for srv._waiter
loop.run_until_complete(asyncio.sleep(0.01))
assert srv._waiter is not None

srv.data_received(
b'GET / HTTP/1.1\r\n'
b'Host: ex.com\r\n'
b'Content-Length: 1\r\n\r\n'
b'a')
srv.data_received(
b'GET / HTTP/1.1\r\n'
b'Host: ex.com\r\n'
b'Content-Length: 1\r\n\r\n'
b'b')

assert len(srv._messages) == 2
assert srv._waiter.done()

0 comments on commit 49a540b

Please sign in to comment.