Skip to content

Commit

Permalink
Treat asyncio resume_reading error as a closed connection (#201)
Browse files Browse the repository at this point in the history
* Treat asyncio `resume_reading` error as a closed connection

* Added the fix from PR

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Co-authored-by: florimondmanca <florimond.manca@gmail.com>

* Apply suggestions from code review

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

* Update httpcore/_backends/asyncio.py

* Update asyncio.py

Co-authored-by: florimondmanca <florimond.manca@gmail.com>
Co-authored-by: Tom Christie <tom@tomchristie.com>
  • Loading branch information
3 people committed Oct 6, 2020
1 parent 995f647 commit d93b6ff
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions httpcore/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,21 @@ async def read(self, n: int, timeout: TimeoutDict) -> bytes:
exc_map = {asyncio.TimeoutError: ReadTimeout, OSError: ReadError}
async with self.read_lock:
with map_exceptions(exc_map):
return await asyncio.wait_for(
self.stream_reader.read(n), timeout.get("read")
)
try:
return await asyncio.wait_for(
self.stream_reader.read(n), timeout.get("read")
)
except AttributeError as exc: # pragma: nocover
if "resume_reading" in str(exc):
# Python's asyncio has a bug that can occur when a
# connection has been closed, while it is paused.
# See: https://github.com/encode/httpx/issues/1213
#
# Returning an empty byte-string to indicate connection
# close will eventually raise an httpcore.RemoteProtocolError
# to the user when this goes through our HTTP parsing layer.
return b""
raise

async def write(self, data: bytes, timeout: TimeoutDict) -> None:
if not data:
Expand Down

0 comments on commit d93b6ff

Please sign in to comment.