Skip to content

The agent that grows with you

fix(a2a): dual-stack (AF_INET6, V6ONLY=0) bind when A2A_HOST=::

chatchap-2nd-of-chartup36a6c85c468a
diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.py
index 67c0d43c323..a4dcb9a19a5 100644
--- a/plugins/platforms/a2a/adapter.py
+++ b/plugins/platforms/a2a/adapter.py
@@ -12,6 +12,7 @@ import json
import logging
import os
import re
+import socket
import threading
import time
import urllib.parse
@@ -132,6 +133,32 @@ def _safe_context_slug(value: str, max_len: int = 96) -> str:
return (slug or "ctx")[:max_len]
+class _DualStackThreadingHTTPServer(ThreadingHTTPServer):
+ """Dual-stack (IPv4 + IPv6) ThreadingHTTPServer for Windows.
+
+ The stdlib ``ThreadingHTTPServer`` is AF_INET-pinned (``address_family =
+ AF_INET``), so it cannot bind an IPv6 host like ``::`` at all:
+ ``getaddrinfo("::", port, AF_INET)`` fails with gaierror 11001. When
+ ``A2A_HOST=::`` we want one socket that answers on both stacks
+ (``AF_INET6`` + ``IPV6_V6ONLY=0``), matching the DM (aiohttp) dual-stack
+ bind on 8642. On non-Windows platforms the same V6ONLY=0 socket gives the
+ same dual-stack behavior.
+ """
+
+ address_family = socket.AF_INET6
+
+ def server_bind(self):
+ if self.address_family == socket.AF_INET6:
+ with contextlib.suppress(
+ OSError,
+ AttributeError, # platform without IPV6_V6ONLY
+ ):
+ self.socket.setsockopt(
+ socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0
+ )
+ super().server_bind()
+
+
class A2ARequestHandler(BaseHTTPRequestHandler):
"""HTTP handler for the A2A JSON-RPC surface; all state lives on ``self.server.adapter``."""
@@ -295,8 +322,12 @@ class A2AAdapter(BasePlatformAdapter):
async def connect(self, **_kwargs) -> bool:
# Capture the gateway loop so the HTTP thread can marshal events via run_coroutine_threadsafe.
self._loop = asyncio.get_running_loop()
+ # A2A_HOST=:: wants one dual-stack (v4+v6) socket. The stdlib
+ # ThreadingHTTPServer is AF_INET-pinned and cannot bind "::" at all,
+ # so swap in the AF_INET6/V6ONLY=0 subclass for that case only.
+ _server_cls = _DualStackThreadingHTTPServer if str(self.host) == "::" else ThreadingHTTPServer
try:
- self._httpd = ThreadingHTTPServer((self.host, self.port), A2ARequestHandler)
+ self._httpd = _server_cls((self.host, self.port), A2ARequestHandler)
except OSError as e:
logger.error("A2A: could not bind %s:%s — %s", self.host, self.port, e)
self._set_fatal_error("bind_failed", f"A2A bind failed: {e}", retryable=True)
diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.py
index 6c01c22c307..41292b8b412 100644
--- a/tests/plugins/test_a2a_plugin.py
+++ b/tests/plugins/test_a2a_plugin.py
@@ -126,6 +126,64 @@ class TestBindSafety:
assert security.A2ASecurityContext.capture().resolve_bind_host() == "localhost"
+ def test_connect_dual_stack_bind_when_host_is_v6_wildcard(self, monkeypatch):
+ """A2A_HOST=:: must answer on BOTH stacks, not just loopback IPv4.
+
+ Regression: the upstream ``ThreadingHTTPServer`` is AF_INET-pinned,
+ so a ``::`` bind fails at ``getaddrinfo("::", port, AF_INET)`` with
+ gaierror 11001 (Windows) and the A2A platform stays down. ``::`` must
+ select the
+ AF_INET6 + ``IPV6_V6ONLY=0`` dual-stack server; other hosts keep the
+ standard AF_INET family.
+ """
+ # Loopback-only: no external services. Skip on hosts with no IPv6
+ # (cannot bind '::' at all) rather than falsely fail them.
+ v6 = socket.socket(socket.AF_INET6)
+ try:
+ v6.bind(("::", 0))
+ except OSError:
+ pytest.skip("host cannot bind IPv6 '::'")
+ finally:
+ v6.close()
+ import contextlib
+
+ from plugins.platforms.a2a.adapter import (
+ _DualStackThreadingHTTPServer,
+ ThreadingHTTPServer,
+ )
+
+ # Family selection: :: -> dual-stack subclass; IPv4 host -> standard family.
+ assert _DualStackThreadingHTTPServer.address_family == socket.AF_INET6
+ assert ThreadingHTTPServer.address_family == socket.AF_INET
+
+ monkeypatch.setenv("A2A_BEARER_TOKEN", "topsecret")
+ monkeypatch.setenv("A2A_HOST", "::")
+ adapter, _base = _make_live_adapter(monkeypatch)
+
+ async def run():
+ assert await adapter.connect() is True
+ try:
+ # The live server socket is the dual-stack one...
+ assert isinstance(adapter._httpd, _DualStackThreadingHTTPServer)
+ assert adapter._httpd.address_family == socket.AF_INET6
+ with contextlib.suppress(
+ OSError,
+ AttributeError, # platform without IPV6_V6ONLY
+ ):
+ assert adapter._httpd.socket.getsockopt(
+ socket.IPPROTO_IPV6, socket.IPV6_V6ONLY
+ ) == 0
+ # ...so IPv6 loopback reaches the same socket via the bound port.
+ _, port = adapter._httpd.server_address[:2]
+ base = f"http://[::1]:{port}"
+ card = await asyncio.to_thread(_get_json, base + "/.well-known/agent-card.json")
+ assert "capabilities" in card
+ finally:
+ await adapter.disconnect()
+
+ asyncio.run(run())
+
+
class TestPeerIdentity:
"""authenticate() maps presented credentials to identities; the body
never asserts who the peer is."""