-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix: add TCP keepalive to default httpx transport to prevent NAT hangs #3368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| from typing_extensions import Unpack, Literal, override, get_origin | ||
|
|
||
| import anyio | ||
| import socket | ||
| import httpx | ||
| import distro | ||
| import pydantic | ||
|
|
@@ -831,11 +832,39 @@ def _idempotency_key(self) -> str: | |
| return f"stainless-python-retry-{uuid.uuid4()}" | ||
|
|
||
|
|
||
| def _build_keepalive_socket_options() -> list[tuple[int, int, int | bool]]: | ||
| """Build socket options for TCP keepalive. | ||
|
|
||
| Enables SO_KEEPALIVE and sets platform-appropriate TCP keepalive | ||
| parameters to prevent NAT gateways from silently dropping idle | ||
| connections during long-running non-streaming requests. | ||
| """ | ||
| opts: list[tuple[int, int, int | bool]] = [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)] | ||
|
|
||
| if hasattr(socket, "TCP_KEEPIDLE"): | ||
| # Linux: seconds before sending the first keepalive probe | ||
| opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 60)) | ||
| elif hasattr(socket, "TCP_KEEPALIVE"): | ||
| # macOS: seconds before sending the first keepalive probe | ||
| opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, 60)) | ||
|
|
||
| if hasattr(socket, "TCP_KEEPINTVL"): | ||
| # Seconds between subsequent keepalive probes | ||
| opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 60)) | ||
|
|
||
| if hasattr(socket, "TCP_KEEPCNT"): | ||
| # Number of unacknowledged probes before declaring the connection dead | ||
| opts.append((socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)) | ||
|
|
||
| return opts | ||
|
|
||
|
|
||
| class _DefaultHttpxClient(httpx.Client): | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| kwargs.setdefault("timeout", DEFAULT_TIMEOUT) | ||
| kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) | ||
| kwargs.setdefault("follow_redirects", True) | ||
| kwargs.setdefault("transport", httpx.HTTPTransport(socket_options=_build_keepalive_socket_options())) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Creating and passing a concrete transport here causes Useful? React with 👍 / 👎. |
||
| super().__init__(**kwargs) | ||
|
|
||
|
|
||
|
|
@@ -1423,6 +1452,7 @@ def __init__(self, **kwargs: Any) -> None: | |
| kwargs.setdefault("timeout", DEFAULT_TIMEOUT) | ||
| kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) | ||
| kwargs.setdefault("follow_redirects", True) | ||
| kwargs.setdefault("transport", httpx.AsyncHTTPTransport(socket_options=_build_keepalive_socket_options())) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The async default client has the same issue: supplying a prebuilt Useful? React with 👍 / 👎. |
||
| super().__init__(**kwargs) | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package still declares
httpx>=0.23.0, <1inpyproject.toml, butsocket_optionswas only added toHTTPTransport/AsyncHTTPTransportin httpx 0.25.0. In environments that satisfy the current dependency with httpx 0.23.x or 0.24.x, constructing the default OpenAI client will raiseTypeError: ... unexpected keyword argument 'socket_options'before any request is made. Please either guard this argument for older httpx versions or bump the minimum dependency.Useful? React with 👍 / 👎.