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

Close socket when read or write timeouts occur #365

Merged
merged 1 commit into from
Jun 15, 2021
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
2 changes: 2 additions & 0 deletions httpcore/_backends/anyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async def read(self, n: int, timeout: TimeoutDict) -> bytes:
with anyio.fail_after(read_timeout):
return await self.stream.receive(n)
except TimeoutError:
await self.stream.aclose()
raise ReadTimeout from None
except BrokenResourceError as exc:
raise ReadError from exc
Expand All @@ -75,6 +76,7 @@ async def write(self, data: bytes, timeout: TimeoutDict) -> None:
with anyio.fail_after(write_timeout):
return await self.stream.send(data)
except TimeoutError:
await self.stream.aclose()
raise WriteTimeout from None
except BrokenResourceError as exc:
raise WriteError from exc
Expand Down
16 changes: 12 additions & 4 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ 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)
try:
with trio.fail_after(read_timeout):
return await self.stream.receive_some(max_bytes=n)
except trio.TooSlowError as exc:
await self.stream.aclose()
raise exc

async def write(self, data: bytes, timeout: TimeoutDict) -> None:
if not data:
Expand All @@ -73,8 +77,12 @@ async def write(self, data: bytes, timeout: TimeoutDict) -> None:

async with self.write_lock:
with map_exceptions(exc_map):
with trio.fail_after(write_timeout):
return await self.stream.send_all(data)
try:
with trio.fail_after(write_timeout):
return await self.stream.send_all(data)
except trio.TooSlowError as exc:
await self.stream.aclose()
raise exc

async def aclose(self) -> None:
async with self.write_lock:
Expand Down