Jump to solution
Details

The Fix

Changed `ClientRequest.connection_key` to be a `NamedTuple` to improve client performance.

Based on closed aio-libs/aiohttp issue #9364 · PR/commit linked

Open PR/Commit
@@ -0,0 +1 @@ @@ -0,0 +1 @@ +Changed ``ClientRequest.connection_key`` to be a `NamedTuple` to improve client performance -- by :user:`bdraco`. diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index d9a7d1cd049..4da944d38dd 100644
fix.md
Option A — Apply the official fix\nChanged `ClientRequest.connection_key` to be a `NamedTuple` to improve client performance.\nWhen NOT to use: This fix is not suitable if the application relies on the specific behavior of dataclass equality.\n\n

Why This Fix Works in Production

  • Trigger: ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x
  • Mechanism: Switching `ConnectionKey` from a dataclass to a NamedTuple improves performance by utilizing native code for hashing

Why This Breaks in Prod

  • Switching `ConnectionKey` from a dataclass to a NamedTuple improves performance by utilizing native code for hashing
  • Production symptom (often without a traceback): ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“Looks like switching it to a NamedTuple will speed it up since the hashing can happen all in native code. Sadly its not cached though…”
@bdraco · 2024-10-01 · source
“Performance regressed in https://github.com/aio-libs/aiohttp/pull/5284”
@bdraco · 2024-10-01 · source
“connect with attrs !attrs_connect connect with namedtuple !namedtuple_connect”
@bdraco · 2024-10-01 · source

Failure Signature (Search String)

  • ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x
Copy-friendly signature
signature.txt
Failure Signature ----------------- ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- ConnectionKey hashing and equality has an unexpected impact on performance between master and 3.x

What Broke

Performance regression observed in connection management leading to slower client requests.

Why It Broke

Switching `ConnectionKey` from a dataclass to a NamedTuple improves performance by utilizing native code for hashing

Fix Options (Details)

Option A — Apply the official fix

Changed `ClientRequest.connection_key` to be a `NamedTuple` to improve client performance.

When NOT to use: This fix is not suitable if the application relies on the specific behavior of dataclass equality.

Fix reference: https://github.com/aio-libs/aiohttp/pull/9365

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not suitable if the application relies on the specific behavior of dataclass equality.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
  • Pin production dependencies and upgrade only with a reproducible test that hits the failing path.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.