diff --git a/httpcore/_backends/asyncio.py b/httpcore/_backends/asyncio.py index c97001a2..ba45f732 100644 --- a/httpcore/_backends/asyncio.py +++ b/httpcore/_backends/asyncio.py @@ -131,9 +131,12 @@ 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( + data = await asyncio.wait_for( self.stream_reader.read(n), timeout.get("read") ) + if data == b"": + raise ReadError("Server disconnected while attempting read") + return data async def write(self, data: bytes, timeout: TimeoutDict) -> None: if not data: diff --git a/httpcore/_backends/sync.py b/httpcore/_backends/sync.py index 619a1c9c..07375cab 100644 --- a/httpcore/_backends/sync.py +++ b/httpcore/_backends/sync.py @@ -59,7 +59,10 @@ def read(self, n: int, timeout: TimeoutDict) -> bytes: with self.read_lock: with map_exceptions(exc_map): self.sock.settimeout(read_timeout) - return self.sock.recv(n) + data = self.sock.recv(n) + if data == b"": + raise ReadError("Server disconnected while attempting read") + return data def write(self, data: bytes, timeout: TimeoutDict) -> None: write_timeout = timeout.get("write") diff --git a/httpcore/_backends/trio.py b/httpcore/_backends/trio.py index 4e369f6d..5f047203 100644 --- a/httpcore/_backends/trio.py +++ b/httpcore/_backends/trio.py @@ -60,7 +60,10 @@ async def read(self, n: int, timeout: TimeoutDict) -> bytes: async with self.read_lock: with map_exceptions(exc_map): with trio.fail_after(read_timeout): - return await self.stream.receive_some(max_bytes=n) + data = await self.stream.receive_some(max_bytes=n) + if data == b"": + raise ReadError("Server disconnected while attempting read") + return data async def write(self, data: bytes, timeout: TimeoutDict) -> None: if not data: