{"action":"created","issue":{"url":"https://api.github.com/repos/aio-libs/aioredis/issues/292","repository_url":"https://api.github.com/repos/aio-libs/aioredis","labels_url":"https://api.github.com/repos/aio-libs/aioredis/issues/292/labels{/name}","comments_url":"https://api.github.com/repos/aio-libs/aioredis/issues/292/comments","events_url":"https://api.github.com/repos/aio-libs/aioredis/issues/292/events","html_url":"https://github.com/aio-libs/aioredis/issues/292","id":258719000,"number":292,"title":"CancelledError in conn._reader_task","user":{"login":"honzasp","id":64298,"avatar_url":"https://avatars2.githubusercontent.com/u/64298?v=4","gravatar_id":"","url":"https://api.github.com/users/honzasp","html_url":"https://github.com/honzasp","followers_url":"https://api.github.com/users/honzasp/followers","following_url":"https://api.github.com/users/honzasp/following{/other_user}","gists_url":"https://api.github.com/users/honzasp/gists{/gist_id}","starred_url":"https://api.github.com/users/honzasp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/honzasp/subscriptions","organizations_url":"https://api.github.com/users/honzasp/orgs","repos_url":"https://api.github.com/users/honzasp/repos","events_url":"https://api.github.com/users/honzasp/events{/privacy}","received_events_url":"https://api.github.com/users/honzasp/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-09-19T07:15:57Z","updated_at":"2017-09-28T19:32:11Z","closed_at":null,"author_association":"NONE","body":"I am using a connection pool created with `aioredis.create_redis_pool` and I regularly get a warning from asyncio about unhandled `CancelledError`:\r\n\r\n```\r\nERROR asyncio: Task exception was never retrieved\r\nfuture: <Task finished coro=<RedisConnection._read_data() done, defined at aioredis/connection.py:161> exception=CancelledError()>\r\nconcurrent.futures._base.CancelledError\r\n```\r\n\r\nDigging deeper, I reconstructed the problem as follows:\r\n1. A connection is created and used\r\n2. The connection is not used for some time, and the Redis server disconnects (we connect to AWS ElastiCache)\r\n3. `RedisConnection._read_data()` detects that the server disconnected (triggering the `Connection has been closed by server` debug log) and breaks the loop, calling `self._do_close`\r\n4. `_do_close()` closes the connection, and calls `_reader_task.cancel()`\r\n5. `_read_data()` immediately returns (in the same \"tick\" of the event loop), and finishes `_reader_task`\r\n\r\nNow, `_reader_task` is done, but with `CancelledError` set as exception. However, this exception is not detected anywhere and asyncio produces the warning described above.\r\n\r\n---\r\n\r\nI think this points to two potential problems (and their solutions):\r\n1. When `_read_data()` decides to close the connection, it should call `loop.call_sonn(self._do_close, exc)`, so that `_read_data()` returns and `_reader_task` finishes before `_do_close()` attempts to cancel the task.\r\n2. If `_reader_task` exits with an exception, it is completely ignored. However, I am not sure as to what the expected behaviour should be:\r\n   - Calling `_do_close()` with the exception is probably not desirable, because an exception here signifies a bug in `aioredis` and not an error in the user code. Moreover, the connection may have already closed, which would mean that the exception would be lost silently.\r\n   - Logging the exception to the aioredis logger would mean that most of the time, the exception would be lost without any trace.\r\n   - Relying on the asyncio `exception was never retrieved` warning may ignore the exception if the user disables the debug mode of asyncio.\r\n\r\nI am happy to contribute a PR fixing (1) and possibly (2), but I will be grateful for an advice how to test for this kind of error?"},"comment":{"url":"https://api.github.com/repos/aio-libs/aioredis/issues/comments/332940639","html_url":"https://github.com/aio-libs/aioredis/issues/292#issuecomment-332940639","issue_url":"https://api.github.com/repos/aio-libs/aioredis/issues/292","id":332940639,"user":{"login":"popravich","id":323442,"avatar_url":"https://avatars0.githubusercontent.com/u/323442?v=4","gravatar_id":"","url":"https://api.github.com/users/popravich","html_url":"https://github.com/popravich","followers_url":"https://api.github.com/users/popravich/followers","following_url":"https://api.github.com/users/popravich/following{/other_user}","gists_url":"https://api.github.com/users/popravich/gists{/gist_id}","starred_url":"https://api.github.com/users/popravich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/popravich/subscriptions","organizations_url":"https://api.github.com/users/popravich/orgs","repos_url":"https://api.github.com/users/popravich/repos","events_url":"https://api.github.com/users/popravich/events{/privacy}","received_events_url":"https://api.github.com/users/popravich/received_events","type":"User","site_admin":false},"created_at":"2017-09-28T19:32:11Z","updated_at":"2017-09-28T19:32:11Z","author_association":"MEMBER","body":"Hi, @JendaPlhak \r\nSorry for silence, the project is definitely maintained, but I have not much free time these days.\r\nI've just added few tests for this issue (059c74f907fdc39e3f4fbc62bfeea9edc47416d3) but can't reproduce the warning message"}}
{"actor":{"display_login":"popravich"}}