Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/httpx2/httpx2/_transports/asgi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
Expand Down Expand Up @@ -169,6 +170,11 @@ async def send(message: MutableMapping[str, Any]) -> 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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: Under Trio/AnyIO cancellation, this finally can run in an already-cancelled scope, so await aclose() may be cancelled before an async generator's awaited cleanup finishes. Shield the close with anyio.CancelScope(shield=True).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/httpx2/httpx2/_transports/asgi.py, line 177:

<comment>Under Trio/AnyIO cancellation, this `finally` can run in an already-cancelled scope, so `await aclose()` may be cancelled before an async generator's awaited cleanup finishes. Shield the close with `anyio.CancelScope(shield=True)`.</comment>

<file context>
@@ -169,6 +170,11 @@ async def send(message: MutableMapping[str, Any]) -> None:
+            # 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()
</file context>


assert response_complete.is_set()
assert status_code is not None
Expand Down
74 changes: 73 additions & 1 deletion tests/httpx2/test_asgi.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Loading