tcp_site.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Bind an aiohttp ``TCPSite`` for the HTTP-serving adapters (webhook, api_server): exclusive on macOS,
  2. yet able to rebind over a lingering TIME_WAIT socket right after a gateway restart."""
  3. from __future__ import annotations
  4. import asyncio
  5. import errno
  6. import logging
  7. import socket
  8. import sys
  9. from typing import TYPE_CHECKING, Optional
  10. if TYPE_CHECKING:
  11. from aiohttp import web
  12. from gateway.platforms.shared_ingress import is_wildcard_host
  13. logger = logging.getLogger(__name__)
  14. def has_live_listener(host: str, port: int) -> bool:
  15. """Blocking probe: True when something accepts connections on ``host:port``. Refused = nobody listens;
  16. any other failure (timeout, unroutable) is treated as live so the caller stays exclusive."""
  17. try:
  18. with socket.create_connection((host, port), timeout=1.0):
  19. return True
  20. except ConnectionRefusedError:
  21. return False
  22. except OSError:
  23. return True
  24. async def start_tcp_site(runner: web.BaseRunner, host: Optional[str], port: int, *, log_tag: str) -> web.TCPSite:
  25. """Bind ``host:port`` on ``runner`` and return the started site; raises OSError when unavailable.
  26. SO_REUSEADDR: on macOS (BSD) two wildcard/specific sockets can silently split traffic while
  27. both report success → disable. On Linux it only permits rebinding past TIME_WAIT (a quick
  28. restart would otherwise fail to bind for ~60s) → keep the default.
  29. The macOS exclusive bind also refuses the port while a server-side TIME_WAIT socket lingers
  30. (2*MSL = 30s after the previous gateway closed a connection first — its shutdown, or any
  31. ``Connection: close`` request), so a ``/restart`` re-binding within seconds failed with
  32. EADDRINUSE although nobody was listening. Preventing the TIME_WAIT at close time (SO_LINGER 0)
  33. would reset in-flight senders and misses the per-request case, hence the bind-side retry: for an
  34. explicit host, ``has_live_listener`` refused proves the address is free (the kernel still
  35. rejects an exact duplicate even with SO_REUSEADDR, and a foreign wildcard listener answers the
  36. probe), so one retry with reuse_address=True is safe. A wildcard host keeps the strict path: a
  37. foreign listener on a non-loopback interface could not be probed, so it must keep winning."""
  38. from aiohttp import web
  39. exclusive = sys.platform == "darwin"
  40. site = web.TCPSite(runner, host, port, reuse_address=False if exclusive else None)
  41. try:
  42. await site.start()
  43. except OSError as exc:
  44. if not exclusive or exc.errno != errno.EADDRINUSE or is_wildcard_host(host):
  45. raise
  46. if await asyncio.to_thread(has_live_listener, host, port):
  47. raise
  48. await site.stop() # aiohttp registers a site before binding: drop the dead one from the runner
  49. logger.info("[%s] %s:%d busy without a live listener (TIME_WAIT from the previous gateway); "
  50. "rebinding with SO_REUSEADDR", log_tag, host, port)
  51. site = web.TCPSite(runner, host, port, reuse_address=True)
  52. await site.start()
  53. return site