Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server disconnect behaviour. #164

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ async def receive_events(self, timeout: TimeoutDict) -> None:
Read some data from the network, and update the H2 state.
"""
data = await self.socket.read(self.READ_NUM_BYTES, timeout)
if data == b"":
raise RemoteProtocolError("Server disconnected")

events = self.h2_state.receive_data(data)
for event in events:
event_stream_id = getattr(event, "stream_id", 0)
Expand Down
5 changes: 1 addition & 4 deletions httpcore/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,9 @@ 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):
data = await asyncio.wait_for(
return 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:
Expand Down
5 changes: 1 addition & 4 deletions httpcore/_backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def read(self, n: int, timeout: TimeoutDict) -> bytes:
with self.read_lock:
with map_exceptions(exc_map):
self.sock.settimeout(read_timeout)
data = self.sock.recv(n)
if data == b"":
raise ReadError("Server disconnected while attempting read")
return data
return self.sock.recv(n)

def write(self, data: bytes, timeout: TimeoutDict) -> None:
write_timeout = timeout.get("write")
Expand Down
5 changes: 1 addition & 4 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ 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):
data = await self.stream.receive_some(max_bytes=n)
if data == b"":
raise ReadError("Server disconnected while attempting read")
return data
return await self.stream.receive_some(max_bytes=n)

async def write(self, data: bytes, timeout: TimeoutDict) -> None:
if not data:
Expand Down
3 changes: 3 additions & 0 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ def receive_events(self, timeout: TimeoutDict) -> None:
Read some data from the network, and update the H2 state.
"""
data = self.socket.read(self.READ_NUM_BYTES, timeout)
if data == b"":
raise RemoteProtocolError("Server disconnected")

events = self.h2_state.receive_data(data)
for event in events:
event_stream_id = getattr(event, "stream_id", 0)
Expand Down