Skip to content

Commit

Permalink
Don't raise exceptions on graceless close cases (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie authored Apr 28, 2021
1 parent 341320f commit 3acf913
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
5 changes: 2 additions & 3 deletions httpcore/_backends/anyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from anyio.streams.tls import TLSAttribute, TLSStream

from .._exceptions import (
CloseError,
ConnectError,
ConnectTimeout,
ReadError,
Expand Down Expand Up @@ -101,8 +100,8 @@ async def aclose(self) -> None:
async with self.write_lock:
try:
await self.stream.aclose()
except BrokenResourceError as exc:
raise CloseError from exc
except BrokenResourceError:
pass

def is_readable(self) -> bool:
sock = self.stream.extra(SocketAttribute.raw_socket)
Expand Down
5 changes: 3 additions & 2 deletions httpcore/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from .. import _utils
from .._exceptions import (
CloseError,
ConnectError,
ConnectTimeout,
ReadError,
Expand Down Expand Up @@ -186,7 +185,7 @@ async def aclose(self) -> None:
is_ssl = self.stream_writer.get_extra_info("ssl_object") is not None

async with self.write_lock:
with map_exceptions({OSError: CloseError}):
try:
self.stream_writer.close()
if is_ssl:
# Give the connection a chance to write any data in the buffer,
Expand All @@ -196,6 +195,8 @@ async def aclose(self) -> None:
if hasattr(self.stream_writer, "wait_closed"):
# Python 3.7+ only.
await self.stream_writer.wait_closed() # type: ignore
except OSError:
pass

def is_readable(self) -> bool:
transport = self.stream_reader._transport # type: ignore
Expand Down
5 changes: 3 additions & 2 deletions httpcore/_backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Optional, Type

from .._exceptions import (
CloseError,
ConnectError,
ConnectTimeout,
ReadError,
Expand Down Expand Up @@ -74,8 +73,10 @@ def write(self, data: bytes, timeout: TimeoutDict) -> None:

def close(self) -> None:
with self.write_lock:
with map_exceptions({socket.error: CloseError}):
try:
self.sock.close()
except socket.error:
pass

def is_readable(self) -> bool:
return is_socket_readable(self.sock.fileno())
Expand Down
5 changes: 3 additions & 2 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import trio

from .._exceptions import (
CloseError,
ConnectError,
ConnectTimeout,
ReadError,
Expand Down Expand Up @@ -79,8 +78,10 @@ async def write(self, data: bytes, timeout: TimeoutDict) -> None:

async def aclose(self) -> None:
async with self.write_lock:
with map_exceptions({trio.BrokenResourceError: CloseError}):
try:
await self.stream.aclose()
except trio.BrokenResourceError:
pass

def is_readable(self) -> bool:
# Adapted from: https://github.com/encode/httpx/pull/143#issuecomment-515202982
Expand Down

0 comments on commit 3acf913

Please sign in to comment.