fix: enable TCP keepalive on default httpx transports#2706
Open
KlyneChrysler wants to merge 1 commit into
Open
fix: enable TCP keepalive on default httpx transports#2706KlyneChrysler wants to merge 1 commit into
KlyneChrysler wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.