Skip to content

fix: enable TCP keepalive on default httpx transports#2706

Open
KlyneChrysler wants to merge 1 commit into
googleapis:mainfrom
KlyneChrysler:fix/2705-tcp-keepalive
Open

fix: enable TCP keepalive on default httpx transports#2706
KlyneChrysler wants to merge 1 commit into
googleapis:mainfrom
KlyneChrysler:fix/2705-tcp-keepalive

Conversation

@KlyneChrysler

Copy link
Copy Markdown

Fixes #2705

Long running generate_content calls can stay silent on the wire for longer than the idle timeout of NAT gateways and stateful firewalls on the egress path. Without keepalive probes the connection is dropped silently, so the read hangs forever with no timeout set, or raises ReadTimeout when one is set. Likely the same root cause as #1893.

This enables SO_KEEPALIVE with a 60 second idle time on the default sync and async httpx transports, matching the approach the Anthropic Python SDK uses by default. The socket option names are platform dependent, so the helper guards each one: TCP_KEEPIDLE on Linux and Windows, TCP_KEEPALIVE on macOS, plus TCP_KEEPINTVL and TCP_KEEPCNT where available.

User supplied transports and mounts are left untouched, and proxy configuration keeps working since httpx mounts proxies over the base transport, which I verified empirically.

Five new tests cover the helper, the default transports for both clients, and preservation of user supplied transports. The full tests/client directory passes, 170 passed. Mypy reports no new errors on the changed file.

Long running generate_content calls can stay silent on the wire for
longer than the idle timeout of NAT gateways and stateful firewalls,
which silently drop the connection and leave the response read hanging
forever. Enable SO_KEEPALIVE with a 60 second idle time on the default
sync and async transports, with platform guards for the option names.
User supplied transports and mounts are left untouched, and proxy
configuration keeps working since httpx mounts proxies over the base
transport.

Fixes googleapis#2705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

heavy calls die behind NAT idle-eviction because the default httpx transport doesn't set any TCP keepalive options

1 participant