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

Improve handling of server disconnects #112

Merged
merged 4 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion httpcore/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 OSError("Server disconnected while attempting read")
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
return data

async def write(self, data: bytes, timeout: TimeoutDict) -> None:
if not data:
Expand Down
7 changes: 5 additions & 2 deletions httpcore/_backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ def start_tls(

def read(self, n: int, timeout: TimeoutDict) -> bytes:
read_timeout = timeout.get("read")
exc_map = {socket.timeout: ReadTimeout, socket.error: ReadError}
exc_map = {socket.timeout: ReadTimeout, OSError: ReadError}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

socket.error is a synonym for OSError in Python 3.


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 OSError("Server disconnected while attempting read")
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
return data

def write(self, data: bytes, timeout: TimeoutDict) -> None:
write_timeout = timeout.get("write")
Expand Down
13 changes: 11 additions & 2 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,21 @@ async def start_tls(

async def read(self, n: int, timeout: TimeoutDict) -> bytes:
read_timeout = none_as_inf(timeout.get("read"))
exc_map = {trio.TooSlowError: ReadTimeout, trio.BrokenResourceError: ReadError}
exc_map = {trio.TooSlowError: ReadTimeout, OSError: ReadError}

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)
try:
data = await self.stream.receive_some(max_bytes=n)
except trio.BrokenResourceError:
message = "Server disconnected while attempting read"
JayH5 marked this conversation as resolved.
Show resolved Hide resolved
raise OSError(message) from None
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved

if data == b"":
raise OSError("Server disconnected while attempting read")
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor: you could reuse message here.

florimondmanca marked this conversation as resolved.
Show resolved Hide resolved

return data

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