Jump to solution
Details

The Fix

Replaces the tcp_sockopts parameter with a socket_factory parameter in TCPConnector, allowing custom socket creation before connections.

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

Open PR/Commit
@@ -1,2 +0,0 @@ @@ -1,2 +0,0 @@ -Added ``tcp_sockopts`` to ``TCPConnector`` to allow specifying custom socket options --- by :user:`TimMenninger`. diff --git a/CHANGES/10520.feature.rst b/CHANGES/10520.feature.rst
fix.md
Option A — Apply the official fix\nReplaces the tcp_sockopts parameter with a socket_factory parameter in TCPConnector, allowing custom socket creation before connections.\nWhen NOT to use: This fix should not be used if existing tcp_sockopts functionality is required.\n\n

Why This Fix Works in Production

  • Trigger: I would like to have socket options applied before a connection is made so that they apply to handshake packets (e.g. IP_TOS, IPv6_TCLASS ).
  • Mechanism: The TCPConnector did not allow custom socket options to be set before connection establishment

Why This Breaks in Prod

  • The TCPConnector did not allow custom socket options to be set before connection establishment
  • Production symptom (often without a traceback): I would like to have socket options applied before a connection is made so that they apply to handshake packets (e.g. IP_TOS, IPv6_TCLASS ).

Proof / Evidence

Discussion

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

“@bdraco Should we do the same here, replacing #10474? It's not released yet, so it can still be changed.”
@Dreamsorcerer · 2025-03-06 · confirmation · source
“I think it makes sense to use the same pattern here.”
@bdraco · 2025-03-06 · source

Failure Signature (Search String)

  • I would like to have socket options applied before a connection is made so that they apply to handshake packets (e.g. IP_TOS, IPv6_TCLASS ).
  • The existing tcp_sockopts could be repurposed for this, however, passing in socket options like this doesn't allow one to set different options depending on the destination
Copy-friendly signature
signature.txt
Failure Signature ----------------- I would like to have socket options applied before a connection is made so that they apply to handshake packets (e.g. IP_TOS, IPv6_TCLASS ). The existing tcp_sockopts could be repurposed for this, however, passing in socket options like this doesn't allow one to set different options depending on the destination address (e.g., IPv4 vs IPv6).

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- I would like to have socket options applied before a connection is made so that they apply to handshake packets (e.g. IP_TOS, IPv6_TCLASS ). The existing tcp_sockopts could be repurposed for this, however, passing in socket options like this doesn't allow one to set different options depending on the destination address (e.g., IPv4 vs IPv6).

What Broke

Users were unable to apply socket options to handshake packets, affecting connection performance.

Why It Broke

The TCPConnector did not allow custom socket options to be set before connection establishment

Fix Options (Details)

Option A — Apply the official fix

Replaces the tcp_sockopts parameter with a socket_factory parameter in TCPConnector, allowing custom socket creation before connections.

When NOT to use: This fix should not be used if existing tcp_sockopts functionality is required.

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

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 should not be used if existing tcp_sockopts functionality is required.

Did This Fix Work in Your Case?

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

Prevention

  • Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
  • Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.

Related Issues

No related fixes found.

Sources

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