# Buggy code
async def _send_single_request(self, request: Request, timeout: Timeout) -> Response:
    """
    Sends a single request, without handling any redirections.
    """
    transport = self._transport_for_url(request.url)
    timer = Timer()
    await timer.async_start()

    with map_exceptions(HTTPCORE_EXC_MAP, request=request):
        (status_code, headers, stream, ext) = await transport.arequest(
            request.method.encode(),
            request.url.raw,
            headers=request.headers.raw,
            stream=request.stream,  # type: ignore
            ext={"timeout": timeout.as_dict()},
        )

    async def on_close(response: Response) -> None:
        response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
        if hasattr(stream, "aclose"):
            await stream.aclose()

    response = Response(
        status_code,
        headers=headers,
        stream=stream,  # type: ignore
        ext=ext,
        request=request,
        on_close=on_close,
    )

    self.cookies.extract_cookies(response)

    status = f"{response.status_code} {response.reason_phrase}"
    response_line = f"{response.http_version} {status}"
    logger.debug(f'HTTP Request: {request.method} {request.url} "{response_line}"')

    return response


# Traceback (OSError)
.tox/py38/lib/python3.8/site-packages/httpx/_client.py:1846: in __aexit__
await self.response.aclose()
.tox/py38/lib/python3.8/site-packages/httpx/_models.py:1299: in aclose
await self._on_close(self)
.tox/py38/lib/python3.8/site-packages/httpx/_client.py:1513: in on_close
await stream.aclose()
.tox/py38/lib/python3.8/site-packages/httpcore/_async/connection_pool.py:69: in aclose
await self.stream.aclose()
.tox/py38/lib/python3.8/site-packages/httpcore/_bytestreams.py:78: in aclose
await self._aclose_func()
.tox/py38/lib/python3.8/site-packages/httpcore/_async/http11.py:192: in _response_closed
await self.aclose()
.tox/py38/lib/python3.8/site-packages/httpcore/_async/http11.py:202: in aclose
await self.socket.aclose()
.tox/py38/lib/python3.8/site-packages/httpcore/_backends/asyncio.py:198: in aclose
await self.stream_writer.wait_closed()  # type: ignore
/usr/lib64/python3.8/contextlib.py:131: in __exit__
self.gen.throw(type, value, traceback)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

map = {<class 'OSError'>: <class 'httpcore.CloseError'>}

@contextlib.contextmanager
def map_exceptions(map: Dict[Type[Exception], Type[Exception]]) -> Iterator[None]:
try:
yield
except Exception as exc:  # noqa: PIE786
for from_exc, to_exc in map.items():
if isinstance(exc, from_exc):
raise to_exc(exc) from None
E                   httpcore.CloseError: [Errno 104] Connection reset by peer

.tox/py38/lib/python3.8/site-packages/httpcore/_exceptions.py:12: CloseError
------------------------------------------------- Captured log call --------------------------------------------------
ERROR    asyncio:base_events.py:1707 Exception in callback _SelectorSocketTransport._call_connection_lost(None)
handle: <Handle _SelectorSocketTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "/usr/lib64/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/lib64/python3.8/asyncio/selector_events.py", line 970, in _call_connection_lost
super()._call_connection_lost(exc)
File "/usr/lib64/python3.8/asyncio/selector_events.py", line 730, in _call_connection_lost
self._sock.close()
File "/usr/lib64/python3.8/socket.py", line 500, in close
self._real_close()
File "/usr/lib64/python3.8/socket.py", line 494, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor

# Fixed code
async def _send_single_request(self, request: Request, timeout: Timeout) -> Response:
    """
    Sends a single request, without handling any redirections.
    """
    transport = self._transport_for_url(request.url)
    timer = Timer()
    await timer.async_start()

    with map_exceptions(HTTPCORE_EXC_MAP, request=request):
        (status_code, headers, stream, ext) = await transport.arequest(
            request.method.encode(),
            request.url.raw,
            headers=request.headers.raw,
            stream=request.stream,  # type: ignore
            ext={"timeout": timeout.as_dict()},
        )

    async def on_close(response: Response) -> None:
        response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
        if hasattr(stream, "aclose"):
            with map_exceptions(HTTPCORE_EXC_MAP, request=request):
                await stream.aclose()

    response = Response(
        status_code,
        headers=headers,
        stream=stream,  # type: ignore
        ext=ext,
        request=request,
        on_close=on_close,
    )

    self.cookies.extract_cookies(response)

    status = f"{response.status_code} {response.reason_phrase}"
    response_line = f"{response.http_version} {status}"
    logger.debug(f'HTTP Request: {request.method} {request.url} "{response_line}"')

    return response