Skip to content

Add asyncio network backend - #1303

Open
mimi89999 wants to merge 1 commit into
pydantic:mainfrom
mimi89999:asyncio-backend
Open

mimi89999 wants to merge 1 commit into
pydantic:mainfrom
mimi89999:asyncio-backend

Conversation

@mimi89999

@mimi89999 mimi89999 commented Oct 30, 2026 •

Copy link
Copy Markdown

Summary

Adds httpcore2.AsyncIOBackend, a network backend built directly on asyncio streams, without anyio. It's opt-in for now: AutoBackend still uses AnyIOBackend.

It also does its own happy eyeballs (RFC 8305), so it behaves the same on uvloop, which doesn't support happy_eyeballs_delay.

The stale connection check first looks at what asyncio has already read (EOF, error or buffered data), and only polls the socket if there's nothing there.

Idle connections (time per request, median of 3 rounds, rebased on #1352):

Idle anyio asyncio Change
1 386 µs 351 µs -9%
10 411 µs 366 µs -11%
100 579 µs 508 µs -12%
500 1339 µs 1284 µs -4%

benchmark/run.py --quick (Python 3.14, median of 3 rounds, with a local tweak to pick the backend):

Scenario httpcore2 rps httpx2 rps
c16/1k 1,999 → 2,505 (+25%) 1,813 → 1,936 (+7%)
c512/1k 872 → 933 (+7%) 819 → 783 (-4%)
c64/100k 1,358 → 1,701 (+25%) 1,219 → 1,450 (+19%)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

View guided diff

@codspeed

codspeed Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing mimi89999:asyncio-backend (409c425) with main (674d4cb)

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. ↩

@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.

1 issue found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/httpcore2/httpcore2/_backends/asyncio.py">

<violation number="1" location="src/httpcore2/httpcore2/_backends/asyncio.py:201">
P2: A hostname `local_address` can force the destination lookup to the resolver's first family and prevent valid connections to the other family. Resolve or bind the local source address per destination-family attempt instead of restricting all remote addresses to the first local result.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

family: int = socket.AF_UNSPEC
local_addr = None
if local_address is not None:
family, *_, local_addr = (await _getaddrinfo(local_address, 0, socket.AF_UNSPEC))[0]

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: A hostname local_address can force the destination lookup to the resolver's first family and prevent valid connections to the other family. Resolve or bind the local source address per destination-family attempt instead of restricting all remote addresses to the first local result.

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/httpcore2/httpcore2/_backends/asyncio.py, line 201:

<comment>A hostname `local_address` can force the destination lookup to the resolver's first family and prevent valid connections to the other family. Resolve or bind the local source address per destination-family attempt instead of restricting all remote addresses to the first local result.</comment>

<file context>
@@ -0,0 +1,287 @@
+    family: int = socket.AF_UNSPEC
+    local_addr = None
+    if local_address is not None:
+        family, *_, local_addr = (await _getaddrinfo(local_address, 0, socket.AF_UNSPEC))[0]
+    infos = _interleave_families(await _getaddrinfo(host, port, family))
+
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's the same behavior as in anyio

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@cubic-dev-ai Can I leave it like that?

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.

@mimi89999 You're right — I verified it, and anyio does exactly this. In anyio._core._sockets.connect_tcp:

family = socket.AF_UNSPEC
if local_host:
    gai_res = await getaddrinfo(str(local_host), None)
    family, *_, local_address = gai_res[0]
...
gai_res = await getaddrinfo(target_host, remote_port, family=family, ...)

Same pattern: resolve the local host with AF_UNSPEC, take the first entry, and restrict the destination lookup to that family. So my comment is too broad for this PR — this backend is meant to be behaviorally equivalent to the default AnyIOBackend, and deviating here would actually make it less consistent with the anyio path. Leaving it as-is is defensible; feel free to resolve the thread. If you wanted, a one-line # same as anyio comment next to that block would save the next reviewer the same investigation.

One tiny nuance if you care: for remote IP literals, anyio bypasses the family restriction entirely (it checks ip_address(remote_host) before getaddrinfo and connects directly), while your _getaddrinfo applies the restricted family to literals too. So a hostname local_address that resolves IPv6-first plus an IPv4-literal destination would connect under anyio but fail here. That's a very narrow corner, and not worth changing if you don't want to — I just mention it so the parity claim is exact.

Tip: get faster answers by chatting with cubic’s review copilot in the review UI

@Kludex

Kludex commented Oct 30, 2026

Copy link
Copy Markdown
Member

Please fix CI.

@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 4 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

Comment thread tests/httpcore2/test_asyncio_backend.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 2 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

@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).

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

View guided diff | Re-trigger cubic

Comment thread tests/httpcore2/test_asyncio_backend.py Resolved
@mimi89999

Copy link
Copy Markdown
Author

@Kludex I added tests, fixed the CI and addressed the bot’s findings. I also removed the poll() call from the stale connection check, which brings the 500 idle connections case to -75% vs anyio.

@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 1 file (changes from recent commits).

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

View guided diff | Re-trigger cubic

@veria-ai

veria-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

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.

2 participants