Skip to content

Stream response bodies in ASGITransport - #1269

Open
Kludex wants to merge 14 commits into
async-client-transport-lifecyclefrom
asgi-streaming-transport-task-group
Open

Kludex wants to merge 14 commits into
async-client-transport-lifecyclefrom
asgi-streaming-transport-task-group

Conversation

@Kludex

@Kludex Kludex commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

ASGITransport now streams response bodies. Inside async with, the app runs in a task group owned by the transport, and the response is returned as soon as the app sends http.response.start. This makes it possible to test SSE and other endpoints that never finish.

transport = httpx2.ASGITransport(app=app)
async with httpx2.AsyncClient(transport=transport, base_url="http://testserver") as client:
    async with client.stream("GET", "/events") as response:
        async for line in response.aiter_lines():
            ...

The task group lives for the lifetime of the transport rather than inside an async generator per request. A task group held across yield breaks when a response isn't closed in the same task. On main, TestASGIWebSocketTransport::test_http fails that way on asyncio and on trio.

  • Closing the response sends http.disconnect to the app. Apps still running when the transport exits are cancelled.
  • Without async with, the app runs to completion before the response is returned, as before.
  • An exception raised before http.response.start is raised from the request. One raised after it is raised while reading the body, so client.get() still raises it.
  • ASGIWebSocketTransport reuses the base task group instead of managing its own.
  • An app that returns without completing its response raises RuntimeError instead of producing a truncated or synthetic 500 response.

Behaviour changes:

  • Inside async with, context variables set by the app are no longer visible to the caller.
  • A streaming response from handle_async_request that is never closed is reported by anyio with a ResourceWarning once it is garbage collected.

Depends on #1270, which makes AsyncClient unwind transports in reverse order, so several transports with task groups can be mounted together. This PR targets that branch until it merges.

Supersedes #1032 and #1171. Addresses the long-standing upstream request encode/httpx#2186.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

View guided diff

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T12:06:11.627840Z 91adc48 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codspeed

codspeed Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing asgi-streaming-transport-task-group (a570ffc) with async-client-transport-lifecycle (f4ab8dc)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 91adc48a80

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

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.

Review completed against the latest diff

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread docs/advanced/transports.md Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

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.

All reported issues were addressed

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread tests/httpx2/test_asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
…ransport-task-group

# Conflicts:
#	tests/httpx2/client/test_async_client.py
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
@Kludex
Kludex changed the base branch from main to async-client-transport-lifecycle October 18, 2026 13:04

@cubic-dev-ai cubic-dev-ai Bot left a comment •

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.

All reported issues were addressed across 2 files (changes from recent commits).

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/httpx2/test_asgi.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

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.

All reported issues were addressed across 5 files

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved
Comment thread src/httpx2/httpx2/_transports/asgi.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant