From 8fa4318f14eab40940bedf5bf0afdee4aa80f01c Mon Sep 17 00:00:00 2001 From: Alexey Shokov Date: Mon, 16 Nov 2026 16:20:56 +0400 Subject: [PATCH 1/3] Close inner async iterators when the consumer stops early Several async iterators in httpx2 iterate another one with a bare `async for`. When their consumer stops early (`break`, or an exception between yields) and closes the outer one, the inner async generator is left suspended until the garbage collector finalizes it, on whatever event loop collects it. Trio reports that as a `ResourceWarning`. Affected: `ASGITransport` (the request body, when the app answers without reading it all), `AsyncResponseStream` (httpcore2's stream), `Response.aiter_text`, `Response.aiter_lines` and `EventSource`. Add `safe_async_iterate`, the httpx2 counterpart of httpcore2's helper, and use it at those sites and at the three that closed the inner iterator by hand. --- src/httpx2/httpx2/_client.py | 8 +- src/httpx2/httpx2/_content.py | 10 +- src/httpx2/httpx2/_models.py | 32 +++--- src/httpx2/httpx2/_sse.py | 8 +- src/httpx2/httpx2/_transports/asgi.py | 28 ++--- src/httpx2/httpx2/_transports/default.py | 6 +- src/httpx2/httpx2/_utils.py | 21 +++- tests/httpx2/test_async_iterator_closing.py | 113 ++++++++++++++++++++ 8 files changed, 178 insertions(+), 48 deletions(-) create mode 100644 tests/httpx2/test_async_iterator_closing.py diff --git a/src/httpx2/httpx2/_client.py b/src/httpx2/httpx2/_client.py index 7ab58b4a7..fd096b4b1 100644 --- a/src/httpx2/httpx2/_client.py +++ b/src/httpx2/httpx2/_client.py @@ -53,7 +53,7 @@ TimeoutTypes, ) from ._urls import URL, QueryParams -from ._utils import URLPattern, get_environment_proxies +from ._utils import URLPattern, get_environment_proxies, safe_async_iterate if TYPE_CHECKING: import ssl # pragma: no cover @@ -155,13 +155,9 @@ def __init__(self, stream: AsyncByteStream, start: float) -> None: self.elapsed: datetime.timedelta | None = None async def __aiter__(self) -> AsyncIterator[bytes]: - stream = self._stream.__aiter__() - try: + async with safe_async_iterate(self._stream) as stream: async for chunk in stream: yield chunk - finally: - if isinstance(stream, AsyncGenerator): - await stream.aclose() async def aclose(self) -> None: self.elapsed = datetime.timedelta(seconds=time.perf_counter() - self._start) diff --git a/src/httpx2/httpx2/_content.py b/src/httpx2/httpx2/_content.py index 3acb2574f..06ffda96a 100644 --- a/src/httpx2/httpx2/_content.py +++ b/src/httpx2/httpx2/_content.py @@ -2,7 +2,7 @@ import inspect import warnings -from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping +from collections.abc import AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping from json import dumps as json_dumps from typing import ( Any, @@ -19,7 +19,7 @@ ResponseContent, SyncByteStream, ) -from ._utils import peek_filelike_remaining_length, primitive_value_to_str +from ._utils import peek_filelike_remaining_length, primitive_value_to_str, safe_async_iterate __all__ = ["ByteStream"] @@ -81,13 +81,9 @@ async def __aiter__(self) -> AsyncIterator[bytes]: else: # Otherwise iterate, making sure the wrapped stream is closed even if the # consumer stops early (e.g. an exception is raised part-way through decoding). - stream = self._stream.__aiter__() - try: + async with safe_async_iterate(self._stream) as stream: async for part in stream: yield part - finally: - if isinstance(stream, AsyncGenerator): - await stream.aclose() class UnattachedStream(AsyncByteStream, SyncByteStream): diff --git a/src/httpx2/httpx2/_models.py b/src/httpx2/httpx2/_models.py index 70d34e42b..6c33b13b4 100644 --- a/src/httpx2/httpx2/_models.py +++ b/src/httpx2/httpx2/_models.py @@ -60,7 +60,7 @@ SyncByteStream, ) from ._urls import URL -from ._utils import to_bytes_or_str, to_str +from ._utils import safe_async_iterate, to_bytes_or_str, to_str __all__ = ["Cookies", "Headers", "Request", "Response"] @@ -1023,10 +1023,11 @@ async def aiter_text(self, chunk_size: int | None = None) -> AsyncIterator[str]: decoder = TextDecoder(encoding=self.encoding or "utf-8") chunker = TextChunker(chunk_size=chunk_size) with request_context(request=self._request): - async for byte_content in self.aiter_bytes(): - text_content = decoder.decode(byte_content) - for chunk in chunker.decode(text_content): - yield chunk + async with safe_async_iterate(self.aiter_bytes()) as byte_stream: + async for byte_content in byte_stream: + text_content = decoder.decode(byte_content) + for chunk in chunker.decode(text_content): + yield chunk text_content = decoder.flush() for chunk in chunker.decode(text_content): yield chunk # pragma: no cover @@ -1036,9 +1037,10 @@ async def aiter_text(self, chunk_size: int | None = None) -> AsyncIterator[str]: async def aiter_lines(self) -> AsyncIterator[str]: decoder = LineDecoder() with request_context(request=self._request): - async for text in self.aiter_text(): - for line in decoder.decode(text): - yield line + async with safe_async_iterate(self.aiter_text()) as text_stream: + async for text in text_stream: + for line in decoder.decode(text): + yield line for line in decoder.flush(): yield line @@ -1057,19 +1059,17 @@ async def aiter_raw(self, chunk_size: int | None = None) -> AsyncGenerator[bytes self._num_bytes_downloaded = 0 chunker = ByteChunker(chunk_size=chunk_size) - stream = self.stream.__aiter__() try: - with request_context(request=self._request): - async for raw_stream_bytes in stream: - self._num_bytes_downloaded += len(raw_stream_bytes) - for chunk in chunker.decode(raw_stream_bytes): - yield chunk + async with safe_async_iterate(self.stream) as stream: + with request_context(request=self._request): + async for raw_stream_bytes in stream: + self._num_bytes_downloaded += len(raw_stream_bytes) + for chunk in chunker.decode(raw_stream_bytes): + yield chunk for chunk in chunker.flush(): yield chunk finally: - if isinstance(stream, AsyncGenerator): - await stream.aclose() await self.aclose() async def aclose(self) -> None: diff --git a/src/httpx2/httpx2/_sse.py b/src/httpx2/httpx2/_sse.py index df2be6aa6..33edaf7fb 100644 --- a/src/httpx2/httpx2/_sse.py +++ b/src/httpx2/httpx2/_sse.py @@ -35,6 +35,7 @@ from ._config import DEFAULT_MAX_EVENT_SIZE_BYTES from ._exceptions import TransportError, request_context from ._models import Response +from ._utils import safe_async_iterate __all__ = ["EventSource", "SSEError", "ServerSentEvent"] @@ -219,8 +220,9 @@ async def __aiter__(self) -> AsyncIterator[ServerSentEvent]: with request_context(request=self._response.request): self._check_content_type() parser = _SSEParser(self._max_event_size) - async for chunk in self._response.aiter_text(): - for sse in parser.decode(chunk): - yield sse + async with safe_async_iterate(self._response.aiter_text()) as text_stream: + async for chunk in text_stream: + for sse in parser.decode(chunk): + yield sse for sse in parser.flush(): yield sse diff --git a/src/httpx2/httpx2/_transports/asgi.py b/src/httpx2/httpx2/_transports/asgi.py index 1b78554ac..c490b7f51 100644 --- a/src/httpx2/httpx2/_transports/asgi.py +++ b/src/httpx2/httpx2/_transports/asgi.py @@ -5,6 +5,7 @@ from .._models import Request, Response from .._types import AsyncByteStream +from .._utils import safe_async_iterate from .base import AsyncBaseTransport if TYPE_CHECKING: @@ -110,8 +111,7 @@ async def handle_async_request(self, request: Request) -> Response: "root_path": self.root_path, } - # Request. - request_body_chunks = request.stream.__aiter__() + # Request. `request_body_chunks` is bound below, around the app call. request_complete = False # Response. @@ -158,17 +158,19 @@ async def send(message: MutableMapping[str, Any]) -> None: if not more_body: response_complete.set() - try: - await self.app(scope, receive, send) - except Exception: - if self.raise_app_exceptions: - raise - - response_complete.set() - if status_code is None: - status_code = 500 - if response_headers is None: - response_headers = {} + # The app may answer without reading the whole body; close it once the app returns. + async with safe_async_iterate(request.stream) as request_body_chunks: + try: + await self.app(scope, receive, send) + except Exception: + if self.raise_app_exceptions: + raise + + response_complete.set() + if status_code is None: + status_code = 500 + if response_headers is None: + response_headers = {} assert response_complete.is_set() assert status_code is not None diff --git a/src/httpx2/httpx2/_transports/default.py b/src/httpx2/httpx2/_transports/default.py index 9afc18ecb..907debfff 100644 --- a/src/httpx2/httpx2/_transports/default.py +++ b/src/httpx2/httpx2/_transports/default.py @@ -56,6 +56,7 @@ from .._models import Request, Response from .._types import AsyncByteStream, CertTypes, ProxyTypes, SyncByteStream from .._urls import URL +from .._utils import safe_async_iterate from .base import AsyncBaseTransport, BaseTransport SOCKET_OPTION = tuple[int, int, int] | tuple[int, int, bytes | bytearray] | tuple[int, int, None, int] @@ -260,8 +261,9 @@ def __init__(self, httpcore_stream: AsyncIterable[bytes]) -> None: async def __aiter__(self) -> AsyncIterator[bytes]: with map_httpcore_exceptions(): - async for part in self._httpcore_stream: - yield part + async with safe_async_iterate(self._httpcore_stream) as parts: + async for part in parts: + yield part async def aclose(self) -> None: if hasattr(self._httpcore_stream, "aclose"): diff --git a/src/httpx2/httpx2/_utils.py b/src/httpx2/httpx2/_utils.py index 05b3d44fc..30230d69f 100644 --- a/src/httpx2/httpx2/_utils.py +++ b/src/httpx2/httpx2/_utils.py @@ -3,16 +3,35 @@ import ipaddress import os import re +from collections.abc import AsyncGenerator, AsyncIterable +from contextlib import AbstractAsyncContextManager, aclosing, nullcontext from re import Pattern -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, TypeVar from urllib.request import getproxies from ._exceptions import InvalidURL from ._types import AnyStr, PrimitiveData if TYPE_CHECKING: + from collections.abc import AsyncIterator + from ._urls import URL +T = TypeVar("T") + + +def safe_async_iterate(iterable: AsyncIterable[T], /) -> AbstractAsyncContextManager[AsyncIterator[T]]: + """ + Iterate `iterable` and close its iterator on exit, even if the consumer stops early. + + An async generator left suspended is otherwise finalized by the garbage collector, + on whatever event loop collects it. + """ + iterator = aiter(iterable) + if isinstance(iterator, AsyncGenerator): + return aclosing(iterator) + return nullcontext(iterator) + def primitive_value_to_str(value: PrimitiveData) -> str: """ diff --git a/tests/httpx2/test_async_iterator_closing.py b/tests/httpx2/test_async_iterator_closing.py new file mode 100644 index 000000000..47d403974 --- /dev/null +++ b/tests/httpx2/test_async_iterator_closing.py @@ -0,0 +1,113 @@ +""" +Every async iterator that httpx2 takes from another stream is closed when its consumer stops early. + +Otherwise the inner async generator stays suspended until the garbage collector finalizes it, on whatever +event loop collects it (Trio reports a `ResourceWarning` for that). +""" + +from __future__ import annotations + +from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, MutableMapping +from contextlib import aclosing +from typing import Any + +import pytest + +import httpx2 +from httpx2._transports.default import AsyncResponseStream + +REQUEST = httpx2.Request("GET", "http://www.example.org/") + + +class RecordedStream(httpx2.AsyncByteStream): + def __init__(self, chunk: bytes = b"line\n") -> None: + self._chunk = chunk + self.closed_early = False + + async def __aiter__(self) -> AsyncIterator[bytes]: + try: + for _ in range(16): + yield self._chunk + except GeneratorExit: + self.closed_early = True + raise + + +async def take_first(iterator: AsyncIterator[Any]) -> None: + assert isinstance(iterator, AsyncGenerator) + async with aclosing(iterator): + async for _ in iterator: + break + + +@pytest.mark.anyio +@pytest.mark.parametrize("method", ["aiter_raw", "aiter_bytes", "aiter_text", "aiter_lines"]) +async def test_response_iterator_closes_the_stream(method: str) -> None: + stream = RecordedStream() + response = httpx2.Response(200, stream=stream, request=REQUEST) + + await take_first(getattr(response, method)()) + + assert stream.closed_early + + +@pytest.mark.anyio +async def test_event_source_closes_the_stream() -> None: + stream = RecordedStream(b"data: hello\n\n") + response = httpx2.Response(200, headers={"Content-Type": "text/event-stream"}, stream=stream, request=REQUEST) + + await take_first(aiter(httpx2.EventSource(response))) + + assert stream.closed_early + + +@pytest.mark.anyio +async def test_transport_response_stream_closes_the_httpcore_stream() -> None: + stream = RecordedStream() + + await take_first(aiter(AsyncResponseStream(stream))) + + assert stream.closed_early + + +@pytest.mark.anyio +async def test_asgi_transport_closes_the_unread_request_body() -> None: + async def app( + scope: MutableMapping[str, Any], + receive: Callable[[], Awaitable[MutableMapping[str, Any]]], + send: Callable[[MutableMapping[str, Any]], Awaitable[None]], + ) -> None: + await receive() # the first chunk only + await send({"type": "http.response.start", "status": 413, "headers": []}) + await send({"type": "http.response.body", "body": b""}) + + stream = RecordedStream() + transport = httpx2.ASGITransport(app=app) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=stream)) + + assert response.status_code == 413 + assert stream.closed_early + + +class ChunkIterator: + """A plain async iterator, with no `aclose()` to call.""" + + def __init__(self, chunks: list[bytes]) -> None: + self._chunks = iter(chunks) + + def __aiter__(self) -> ChunkIterator: + return self + + async def __anext__(self) -> bytes: + try: + return next(self._chunks) + except StopIteration: + raise StopAsyncIteration from None + + +@pytest.mark.anyio +async def test_plain_async_iterator_is_read_as_is() -> None: + response = httpx2.Response(200, content=ChunkIterator([b"Hello, ", b"world!"]), request=REQUEST) + + assert await response.aread() == b"Hello, world!" From ca52acb390846d6034822e273fbb656048f1db91 Mon Sep 17 00:00:00 2001 From: Alexey Shokov Date: Mon, 16 Nov 2026 17:04:50 +0400 Subject: [PATCH 2/3] Leave an ASGI request body that a task is still receiving Closing the request body with `safe_async_iterate` around the app call broke apps that leave a task in `receive()` after they return: `aclose()` on a running async generator raises `RuntimeError`, so the request failed where it used to succeed. Close the body in the existing `finally` instead, unless it is running; that task still owns it. Also test the closing in `BoundAsyncStream`, `AsyncIteratorByteStream` and an ASGI app that raises. --- src/httpx2/httpx2/_transports/asgi.py | 34 ++++++----- tests/httpx2/test_async_iterator_closing.py | 64 +++++++++++++++++++++ 2 files changed, 83 insertions(+), 15 deletions(-) diff --git a/src/httpx2/httpx2/_transports/asgi.py b/src/httpx2/httpx2/_transports/asgi.py index c490b7f51..4cabdd4bc 100644 --- a/src/httpx2/httpx2/_transports/asgi.py +++ b/src/httpx2/httpx2/_transports/asgi.py @@ -1,11 +1,11 @@ from __future__ import annotations from collections.abc import AsyncIterator, Awaitable, Callable, MutableMapping +from types import AsyncGeneratorType from typing import TYPE_CHECKING, Any from .._models import Request, Response from .._types import AsyncByteStream -from .._utils import safe_async_iterate from .base import AsyncBaseTransport if TYPE_CHECKING: @@ -111,7 +111,8 @@ async def handle_async_request(self, request: Request) -> Response: "root_path": self.root_path, } - # Request. `request_body_chunks` is bound below, around the app call. + # Request. + request_body_chunks = request.stream.__aiter__() request_complete = False # Response. @@ -158,19 +159,22 @@ async def send(message: MutableMapping[str, Any]) -> None: if not more_body: response_complete.set() - # The app may answer without reading the whole body; close it once the app returns. - async with safe_async_iterate(request.stream) as request_body_chunks: - try: - await self.app(scope, receive, send) - except Exception: - if self.raise_app_exceptions: - raise - - response_complete.set() - if status_code is None: - status_code = 500 - if response_headers is None: - response_headers = {} + try: + await self.app(scope, receive, send) + except Exception: + if self.raise_app_exceptions: + raise + + response_complete.set() + if status_code is None: + status_code = 500 + if response_headers is None: + response_headers = {} + finally: + # The app may answer without reading the whole body: close it, unless a task the app left + # in `receive()` is still reading it (closing a running generator raises). + if isinstance(request_body_chunks, AsyncGeneratorType) and not request_body_chunks.ag_running: + await request_body_chunks.aclose() assert response_complete.is_set() assert status_code is not None diff --git a/tests/httpx2/test_async_iterator_closing.py b/tests/httpx2/test_async_iterator_closing.py index 47d403974..0127c5705 100644 --- a/tests/httpx2/test_async_iterator_closing.py +++ b/tests/httpx2/test_async_iterator_closing.py @@ -11,6 +11,7 @@ from contextlib import aclosing from typing import Any +import anyio import pytest import httpx2 @@ -51,6 +52,27 @@ async def test_response_iterator_closes_the_stream(method: str) -> None: assert stream.closed_early +@pytest.mark.anyio +async def test_async_iterator_content_is_closed() -> None: + stream = RecordedStream() + response = httpx2.Response(200, content=aiter(stream), request=REQUEST) + + await take_first(response.aiter_raw()) + + assert stream.closed_early + + +@pytest.mark.anyio +async def test_client_response_stream_closes_the_transport_stream() -> None: + stream = RecordedStream() + transport = httpx2.MockTransport(lambda request: httpx2.Response(200, stream=stream)) + async with httpx2.AsyncClient(transport=transport) as client: + async with client.stream("GET", "http://www.example.org/") as response: + await take_first(response.aiter_raw()) + + assert stream.closed_early + + @pytest.mark.anyio async def test_event_source_closes_the_stream() -> None: stream = RecordedStream(b"data: hello\n\n") @@ -90,6 +112,48 @@ async def app( assert stream.closed_early +@pytest.mark.anyio +async def test_asgi_transport_closes_the_request_body_when_the_app_raises() -> None: + async def app(scope: Any, receive: Callable[[], Awaitable[Any]], send: Any) -> None: + await receive() # the first chunk only + raise RuntimeError() + + stream = RecordedStream() + transport = httpx2.ASGITransport(app=app, raise_app_exceptions=False) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=stream)) + + assert response.status_code == 500 + assert stream.closed_early + + +@pytest.mark.anyio +async def test_asgi_transport_leaves_a_request_body_still_being_received() -> None: + async def slow_body() -> AsyncIterator[bytes]: + yield b"first" + await anyio.sleep(3600) + yield b"never" # pragma: no cover + + async with anyio.create_task_group() as tg: + + async def app(scope: Any, receive: Callable[[], Awaitable[Any]], send: Any) -> None: + async def listen() -> None: + await receive() + + await receive() + tg.start_soon(listen) # outlives the app + await anyio.wait_all_tasks_blocked() + await send({"type": "http.response.start", "status": 200, "headers": []}) + await send({"type": "http.response.body", "body": b""}) + + transport = httpx2.ASGITransport(app=app) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.post("http://www.example.org/", content=slow_body()) + + assert response.status_code == 200 + tg.cancel_scope.cancel() + + class ChunkIterator: """A plain async iterator, with no `aclose()` to call.""" From fb2d98d033e7ad75a788dc8441475754e1dae8ce Mon Sep 17 00:00:00 2001 From: Alexey Shokov Date: Mon, 16 Nov 2026 17:21:08 +0400 Subject: [PATCH 3/3] Narrow the change to the ASGI request body #1212 already closes the nested response iterators (`aiter_text`, `aiter_lines`, SSE, `AsyncResponseStream`), and #1281 proposes the opposite approach for #1280. Leave those to them and keep only the `ASGITransport` request body fix, with its tests in `test_asgi.py`. --- src/httpx2/httpx2/_client.py | 8 +- src/httpx2/httpx2/_content.py | 10 +- src/httpx2/httpx2/_models.py | 32 ++-- src/httpx2/httpx2/_sse.py | 8 +- src/httpx2/httpx2/_transports/default.py | 6 +- src/httpx2/httpx2/_utils.py | 21 +-- tests/httpx2/test_asgi.py | 74 +++++++- tests/httpx2/test_async_iterator_closing.py | 177 -------------------- 8 files changed, 108 insertions(+), 228 deletions(-) delete mode 100644 tests/httpx2/test_async_iterator_closing.py diff --git a/src/httpx2/httpx2/_client.py b/src/httpx2/httpx2/_client.py index fd096b4b1..7ab58b4a7 100644 --- a/src/httpx2/httpx2/_client.py +++ b/src/httpx2/httpx2/_client.py @@ -53,7 +53,7 @@ TimeoutTypes, ) from ._urls import URL, QueryParams -from ._utils import URLPattern, get_environment_proxies, safe_async_iterate +from ._utils import URLPattern, get_environment_proxies if TYPE_CHECKING: import ssl # pragma: no cover @@ -155,9 +155,13 @@ def __init__(self, stream: AsyncByteStream, start: float) -> None: self.elapsed: datetime.timedelta | None = None async def __aiter__(self) -> AsyncIterator[bytes]: - async with safe_async_iterate(self._stream) as stream: + stream = self._stream.__aiter__() + try: async for chunk in stream: yield chunk + finally: + if isinstance(stream, AsyncGenerator): + await stream.aclose() async def aclose(self) -> None: self.elapsed = datetime.timedelta(seconds=time.perf_counter() - self._start) diff --git a/src/httpx2/httpx2/_content.py b/src/httpx2/httpx2/_content.py index 06ffda96a..3acb2574f 100644 --- a/src/httpx2/httpx2/_content.py +++ b/src/httpx2/httpx2/_content.py @@ -2,7 +2,7 @@ import inspect import warnings -from collections.abc import AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping +from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping from json import dumps as json_dumps from typing import ( Any, @@ -19,7 +19,7 @@ ResponseContent, SyncByteStream, ) -from ._utils import peek_filelike_remaining_length, primitive_value_to_str, safe_async_iterate +from ._utils import peek_filelike_remaining_length, primitive_value_to_str __all__ = ["ByteStream"] @@ -81,9 +81,13 @@ async def __aiter__(self) -> AsyncIterator[bytes]: else: # Otherwise iterate, making sure the wrapped stream is closed even if the # consumer stops early (e.g. an exception is raised part-way through decoding). - async with safe_async_iterate(self._stream) as stream: + stream = self._stream.__aiter__() + try: async for part in stream: yield part + finally: + if isinstance(stream, AsyncGenerator): + await stream.aclose() class UnattachedStream(AsyncByteStream, SyncByteStream): diff --git a/src/httpx2/httpx2/_models.py b/src/httpx2/httpx2/_models.py index 6c33b13b4..70d34e42b 100644 --- a/src/httpx2/httpx2/_models.py +++ b/src/httpx2/httpx2/_models.py @@ -60,7 +60,7 @@ SyncByteStream, ) from ._urls import URL -from ._utils import safe_async_iterate, to_bytes_or_str, to_str +from ._utils import to_bytes_or_str, to_str __all__ = ["Cookies", "Headers", "Request", "Response"] @@ -1023,11 +1023,10 @@ async def aiter_text(self, chunk_size: int | None = None) -> AsyncIterator[str]: decoder = TextDecoder(encoding=self.encoding or "utf-8") chunker = TextChunker(chunk_size=chunk_size) with request_context(request=self._request): - async with safe_async_iterate(self.aiter_bytes()) as byte_stream: - async for byte_content in byte_stream: - text_content = decoder.decode(byte_content) - for chunk in chunker.decode(text_content): - yield chunk + async for byte_content in self.aiter_bytes(): + text_content = decoder.decode(byte_content) + for chunk in chunker.decode(text_content): + yield chunk text_content = decoder.flush() for chunk in chunker.decode(text_content): yield chunk # pragma: no cover @@ -1037,10 +1036,9 @@ async def aiter_text(self, chunk_size: int | None = None) -> AsyncIterator[str]: async def aiter_lines(self) -> AsyncIterator[str]: decoder = LineDecoder() with request_context(request=self._request): - async with safe_async_iterate(self.aiter_text()) as text_stream: - async for text in text_stream: - for line in decoder.decode(text): - yield line + async for text in self.aiter_text(): + for line in decoder.decode(text): + yield line for line in decoder.flush(): yield line @@ -1059,17 +1057,19 @@ async def aiter_raw(self, chunk_size: int | None = None) -> AsyncGenerator[bytes self._num_bytes_downloaded = 0 chunker = ByteChunker(chunk_size=chunk_size) + stream = self.stream.__aiter__() try: - async with safe_async_iterate(self.stream) as stream: - with request_context(request=self._request): - async for raw_stream_bytes in stream: - self._num_bytes_downloaded += len(raw_stream_bytes) - for chunk in chunker.decode(raw_stream_bytes): - yield chunk + with request_context(request=self._request): + async for raw_stream_bytes in stream: + self._num_bytes_downloaded += len(raw_stream_bytes) + for chunk in chunker.decode(raw_stream_bytes): + yield chunk for chunk in chunker.flush(): yield chunk finally: + if isinstance(stream, AsyncGenerator): + await stream.aclose() await self.aclose() async def aclose(self) -> None: diff --git a/src/httpx2/httpx2/_sse.py b/src/httpx2/httpx2/_sse.py index 33edaf7fb..df2be6aa6 100644 --- a/src/httpx2/httpx2/_sse.py +++ b/src/httpx2/httpx2/_sse.py @@ -35,7 +35,6 @@ from ._config import DEFAULT_MAX_EVENT_SIZE_BYTES from ._exceptions import TransportError, request_context from ._models import Response -from ._utils import safe_async_iterate __all__ = ["EventSource", "SSEError", "ServerSentEvent"] @@ -220,9 +219,8 @@ async def __aiter__(self) -> AsyncIterator[ServerSentEvent]: with request_context(request=self._response.request): self._check_content_type() parser = _SSEParser(self._max_event_size) - async with safe_async_iterate(self._response.aiter_text()) as text_stream: - async for chunk in text_stream: - for sse in parser.decode(chunk): - yield sse + async for chunk in self._response.aiter_text(): + for sse in parser.decode(chunk): + yield sse for sse in parser.flush(): yield sse diff --git a/src/httpx2/httpx2/_transports/default.py b/src/httpx2/httpx2/_transports/default.py index 907debfff..9afc18ecb 100644 --- a/src/httpx2/httpx2/_transports/default.py +++ b/src/httpx2/httpx2/_transports/default.py @@ -56,7 +56,6 @@ from .._models import Request, Response from .._types import AsyncByteStream, CertTypes, ProxyTypes, SyncByteStream from .._urls import URL -from .._utils import safe_async_iterate from .base import AsyncBaseTransport, BaseTransport SOCKET_OPTION = tuple[int, int, int] | tuple[int, int, bytes | bytearray] | tuple[int, int, None, int] @@ -261,9 +260,8 @@ def __init__(self, httpcore_stream: AsyncIterable[bytes]) -> None: async def __aiter__(self) -> AsyncIterator[bytes]: with map_httpcore_exceptions(): - async with safe_async_iterate(self._httpcore_stream) as parts: - async for part in parts: - yield part + async for part in self._httpcore_stream: + yield part async def aclose(self) -> None: if hasattr(self._httpcore_stream, "aclose"): diff --git a/src/httpx2/httpx2/_utils.py b/src/httpx2/httpx2/_utils.py index 30230d69f..05b3d44fc 100644 --- a/src/httpx2/httpx2/_utils.py +++ b/src/httpx2/httpx2/_utils.py @@ -3,35 +3,16 @@ import ipaddress import os import re -from collections.abc import AsyncGenerator, AsyncIterable -from contextlib import AbstractAsyncContextManager, aclosing, nullcontext from re import Pattern -from typing import TYPE_CHECKING, Any, TypeVar +from typing import TYPE_CHECKING, Any from urllib.request import getproxies from ._exceptions import InvalidURL from ._types import AnyStr, PrimitiveData if TYPE_CHECKING: - from collections.abc import AsyncIterator - from ._urls import URL -T = TypeVar("T") - - -def safe_async_iterate(iterable: AsyncIterable[T], /) -> AbstractAsyncContextManager[AsyncIterator[T]]: - """ - Iterate `iterable` and close its iterator on exit, even if the consumer stops early. - - An async generator left suspended is otherwise finalized by the garbage collector, - on whatever event loop collects it. - """ - iterator = aiter(iterable) - if isinstance(iterator, AsyncGenerator): - return aclosing(iterator) - return nullcontext(iterator) - def primitive_value_to_str(value: PrimitiveData) -> str: """ diff --git a/tests/httpx2/test_asgi.py b/tests/httpx2/test_asgi.py index e2305e3e1..0c3553053 100644 --- a/tests/httpx2/test_asgi.py +++ b/tests/httpx2/test_asgi.py @@ -1,7 +1,8 @@ import json -from collections.abc import Awaitable, Callable, MutableMapping +from collections.abc import AsyncIterator, Awaitable, Callable, MutableMapping from typing import Any +import anyio import pytest import httpx2 @@ -225,3 +226,74 @@ async def test_asgi_exc_no_raise() -> None: response = await client.get("http://www.example.org/") assert response.status_code == 500 + + +class RecordedBody(httpx2.AsyncByteStream): + def __init__(self) -> None: + self.closed_early = False + + async def __aiter__(self) -> AsyncIterator[bytes]: + try: + for _ in range(16): + yield b"x" * 1024 + except GeneratorExit: + self.closed_early = True + raise + + +@pytest.mark.anyio +async def test_asgi_closes_the_unread_request_body() -> None: + async def app(scope: Scope, receive: Receive, send: Send) -> None: + await receive() # the first chunk only + await send({"type": "http.response.start", "status": 413, "headers": []}) + await send({"type": "http.response.body", "body": b""}) + + body = RecordedBody() + transport = httpx2.ASGITransport(app=app) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=body)) + + assert response.status_code == 413 + assert body.closed_early + + +@pytest.mark.anyio +async def test_asgi_closes_the_unread_request_body_when_the_app_raises() -> None: + async def app(scope: Scope, receive: Receive, send: Send) -> None: + await receive() # the first chunk only + raise RuntimeError() + + body = RecordedBody() + transport = httpx2.ASGITransport(app=app, raise_app_exceptions=False) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=body)) + + assert response.status_code == 500 + assert body.closed_early + + +@pytest.mark.anyio +async def test_asgi_leaves_a_request_body_still_being_received() -> None: + async def slow_body() -> AsyncIterator[bytes]: + yield b"first" + await anyio.sleep(3600) + yield b"never" # pragma: no cover + + async with anyio.create_task_group() as tg: + + async def app(scope: Scope, receive: Receive, send: Send) -> None: + async def listen() -> None: + await receive() + + await receive() + tg.start_soon(listen) # outlives the app + await anyio.wait_all_tasks_blocked() + await send({"type": "http.response.start", "status": 200, "headers": []}) + await send({"type": "http.response.body", "body": b""}) + + transport = httpx2.ASGITransport(app=app) + async with httpx2.AsyncClient(transport=transport) as client: + response = await client.post("http://www.example.org/", content=slow_body()) + + assert response.status_code == 200 + tg.cancel_scope.cancel() diff --git a/tests/httpx2/test_async_iterator_closing.py b/tests/httpx2/test_async_iterator_closing.py deleted file mode 100644 index 0127c5705..000000000 --- a/tests/httpx2/test_async_iterator_closing.py +++ /dev/null @@ -1,177 +0,0 @@ -""" -Every async iterator that httpx2 takes from another stream is closed when its consumer stops early. - -Otherwise the inner async generator stays suspended until the garbage collector finalizes it, on whatever -event loop collects it (Trio reports a `ResourceWarning` for that). -""" - -from __future__ import annotations - -from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, MutableMapping -from contextlib import aclosing -from typing import Any - -import anyio -import pytest - -import httpx2 -from httpx2._transports.default import AsyncResponseStream - -REQUEST = httpx2.Request("GET", "http://www.example.org/") - - -class RecordedStream(httpx2.AsyncByteStream): - def __init__(self, chunk: bytes = b"line\n") -> None: - self._chunk = chunk - self.closed_early = False - - async def __aiter__(self) -> AsyncIterator[bytes]: - try: - for _ in range(16): - yield self._chunk - except GeneratorExit: - self.closed_early = True - raise - - -async def take_first(iterator: AsyncIterator[Any]) -> None: - assert isinstance(iterator, AsyncGenerator) - async with aclosing(iterator): - async for _ in iterator: - break - - -@pytest.mark.anyio -@pytest.mark.parametrize("method", ["aiter_raw", "aiter_bytes", "aiter_text", "aiter_lines"]) -async def test_response_iterator_closes_the_stream(method: str) -> None: - stream = RecordedStream() - response = httpx2.Response(200, stream=stream, request=REQUEST) - - await take_first(getattr(response, method)()) - - assert stream.closed_early - - -@pytest.mark.anyio -async def test_async_iterator_content_is_closed() -> None: - stream = RecordedStream() - response = httpx2.Response(200, content=aiter(stream), request=REQUEST) - - await take_first(response.aiter_raw()) - - assert stream.closed_early - - -@pytest.mark.anyio -async def test_client_response_stream_closes_the_transport_stream() -> None: - stream = RecordedStream() - transport = httpx2.MockTransport(lambda request: httpx2.Response(200, stream=stream)) - async with httpx2.AsyncClient(transport=transport) as client: - async with client.stream("GET", "http://www.example.org/") as response: - await take_first(response.aiter_raw()) - - assert stream.closed_early - - -@pytest.mark.anyio -async def test_event_source_closes_the_stream() -> None: - stream = RecordedStream(b"data: hello\n\n") - response = httpx2.Response(200, headers={"Content-Type": "text/event-stream"}, stream=stream, request=REQUEST) - - await take_first(aiter(httpx2.EventSource(response))) - - assert stream.closed_early - - -@pytest.mark.anyio -async def test_transport_response_stream_closes_the_httpcore_stream() -> None: - stream = RecordedStream() - - await take_first(aiter(AsyncResponseStream(stream))) - - assert stream.closed_early - - -@pytest.mark.anyio -async def test_asgi_transport_closes_the_unread_request_body() -> None: - async def app( - scope: MutableMapping[str, Any], - receive: Callable[[], Awaitable[MutableMapping[str, Any]]], - send: Callable[[MutableMapping[str, Any]], Awaitable[None]], - ) -> None: - await receive() # the first chunk only - await send({"type": "http.response.start", "status": 413, "headers": []}) - await send({"type": "http.response.body", "body": b""}) - - stream = RecordedStream() - transport = httpx2.ASGITransport(app=app) - async with httpx2.AsyncClient(transport=transport) as client: - response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=stream)) - - assert response.status_code == 413 - assert stream.closed_early - - -@pytest.mark.anyio -async def test_asgi_transport_closes_the_request_body_when_the_app_raises() -> None: - async def app(scope: Any, receive: Callable[[], Awaitable[Any]], send: Any) -> None: - await receive() # the first chunk only - raise RuntimeError() - - stream = RecordedStream() - transport = httpx2.ASGITransport(app=app, raise_app_exceptions=False) - async with httpx2.AsyncClient(transport=transport) as client: - response = await client.send(httpx2.Request("POST", "http://www.example.org/", stream=stream)) - - assert response.status_code == 500 - assert stream.closed_early - - -@pytest.mark.anyio -async def test_asgi_transport_leaves_a_request_body_still_being_received() -> None: - async def slow_body() -> AsyncIterator[bytes]: - yield b"first" - await anyio.sleep(3600) - yield b"never" # pragma: no cover - - async with anyio.create_task_group() as tg: - - async def app(scope: Any, receive: Callable[[], Awaitable[Any]], send: Any) -> None: - async def listen() -> None: - await receive() - - await receive() - tg.start_soon(listen) # outlives the app - await anyio.wait_all_tasks_blocked() - await send({"type": "http.response.start", "status": 200, "headers": []}) - await send({"type": "http.response.body", "body": b""}) - - transport = httpx2.ASGITransport(app=app) - async with httpx2.AsyncClient(transport=transport) as client: - response = await client.post("http://www.example.org/", content=slow_body()) - - assert response.status_code == 200 - tg.cancel_scope.cancel() - - -class ChunkIterator: - """A plain async iterator, with no `aclose()` to call.""" - - def __init__(self, chunks: list[bytes]) -> None: - self._chunks = iter(chunks) - - def __aiter__(self) -> ChunkIterator: - return self - - async def __anext__(self) -> bytes: - try: - return next(self._chunks) - except StopIteration: - raise StopAsyncIteration from None - - -@pytest.mark.anyio -async def test_plain_async_iterator_is_read_as_is() -> None: - response = httpx2.Response(200, content=ChunkIterator([b"Hello, ", b"world!"]), request=REQUEST) - - assert await response.aread() == b"Hello, world!"