SG-44724 Fail fast on stale connections instead of hanging - #457
Open
jerrycheng9 wants to merge 2 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #457 +/- ##
==========================================
+ Coverage 78.83% 79.65% +0.82%
==========================================
Files 7 7
Lines 1847 1897 +50
==========================================
+ Hits 1456 1511 +55
+ Misses 391 386 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
dsauve-adsk
approved these changes
Aug 13, 2026
| LOG.debug("Unable to set %s on socket." % option_name, exc_info=True) | ||
|
|
||
|
|
||
| class KeepaliveHTTPConnection(HTTPConnectionWithTimeout): |
Contributor
There was a problem hiding this comment.
nit : Both KeepaliveHTTPConnection and KeepaliveHTTPSConnection are identical, maybe use a mixin to prevent duplication
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.
Problem
shotgun_api3reuses one persistent HTTP(S) connection perShotguninstanceacross all RPC calls (
Shotgun._connection, populated by_get_connection();the underlying socket is cached in
httplib2.Http.connections{}), and everyrequest is sent with
Connection: keep-alive.If a NAT, firewall or load balancer along the path silently drops that idle TCP
session — no FIN, no RST — the client cannot tell. Only
TCP_NODELAYwas set onthe socket; there was no OS-level keepalive and no idle-age tracking on cached
connections. The next call writes into the send buffer successfully and then
blocks in
getresponse()for the fulltimeout_secsbefore it can recover,turning what should be a near-instant reconnect into a multi-hundred-second
hang.
Recovery does not help here, so the fix has to be preventive:
_make_callretries only
ssl.SSLEOFError/ssl.SSLError, while its genericexcept Exceptioncloses the connection and re-raises with no retry, andhttplib2 re-raises
socket.timeoutunconditionally.Changes
Idle expiry (the actual fix):
_Config.max_connection_idle_secs, default 60, which bounds how long acached connection may sit idle before it is closed and recreated instead of
reused. Tunable per instance;
Noneor0disables expiry.Shotgun._connection_last_usedin_http_requestonly after therequest completes. A request that raised is no evidence the socket is alive,
so it must not refresh the idle clock.
_get_connection()closes and rebuilds a stale connection through theexisting
_close_connection(). Both connection consumers (connect()and_http_request) route through_get_connection(), so there is no bypass.TCP keepalive (defense in depth):
KeepaliveHTTPConnection/KeepaliveHTTPSConnectioncallsuper().connect()and then enableSO_KEEPALIVE, tuning idle/interval/countto 30/10/3 so the kernel can notice a vanished peer within roughly a minute of
idling.
SIO_KEEPALIVE_VALSvia ioctl on Windows,TCP_KEEPIDLE/TCP_KEEPALIVEelsewhere, skipping constants that do not existon the platform and swallowing
OSError. BareSO_KEEPALIVEwould not fire aprobe for 7200s on default Linux settings, so the per-platform tuning is what
makes this worth anything — but it is still secondary to the idle expiry.