Skip to content

Cannot cancel reading from incoming connection and retry later #363

Closed
@saghul

Description

In my application I'm doing something like this:

  • Try to receive data
  • Under certain conditions cancel() the task
  • Try to receive data again

This yields the following assertion error:

  File "/Users/saghul/.virtualenvs/cpython342/lib/python3.4/site-packages/aiohttp/web_ws.py", line 206, in receive
    msg = yield from self._reader.read()
  File "/Users/saghul/.virtualenvs/cpython342/lib/python3.4/site-packages/aiohttp/streams.py", line 501, in read
    result = yield from super().read()
  File "/Users/saghul/.virtualenvs/cpython342/lib/python3.4/site-packages/aiohttp/streams.py", line 362, in read
    assert not self._waiter

The problem looks quite obvious, in DataQueue.read the waiter is not reset until feed_data is called: https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/streams.py#L364 and https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/streams.py#L343

The solution, however, is not so obvious, here is a possible solution, let me know if you like the approach and I'll submit a PR with it:

diff --git a/aiohttp/streams.py b/aiohttp/streams.py
index ce0cce4..3879ee6 100644
--- a/aiohttp/streams.py
+++ b/aiohttp/streams.py
@@ -361,7 +361,11 @@ class DataQueue:

             assert not self._waiter
             self._waiter = asyncio.Future(loop=self._loop)
-            yield from self._waiter
+            try:
+                yield from self._waiter
+            except (asyncio.CancelledError, asyncio.TimeoutError):
+                self._waiter = None
+                raise

         if self._buffer:
             data, size = self._buffer.popleft()

For now I'm setting ws._reader._waiter to None after cancelling the task , which is quite ugly... :-S

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions