fix: add socket timeout so a hung OAuth token refresh can't stall the server - #911
Open
ModiZ613 wants to merge 1 commit into
Open
fix: add socket timeout so a hung OAuth token refresh can't stall the server#911ModiZ613 wants to merge 1 commit into
ModiZ613 wants to merge 1 commit into
Conversation
… server _auth.refresh_credentials() (googleapiclient) builds a bare httplib2.Http() with no timeout, and main() sets no process-wide default, so the OAuth token refresh POST runs on an unbounded socket. Normally sub-second, but on a stalled connection (stale keep-alive reuse / transient TLS-TCP hiccup) it blocks indefinitely instead of erroring, tearing down the transport (surfaces as intermittent "Session terminated"). _build_authorized_http() already uses timeout=30, but that does not cover the library's internal refresh path. Set socket.setdefaulttimeout(30) at startup so the timeout-less refresh socket inherits a ceiling. asyncio/uvicorn sockets are non-blocking and unaffected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds a single line in ChangesDefault Socket Timeout
Estimated code review effort: 1 (Trivial) | ~3 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Running in service-account / domain-wide-delegation mode, we hit intermittent
Session terminatederrors reaching clients. Server logs showed the Google API call succeeding, immediately followed by:…which then hung with no completion, tearing down the transport before results returned. It was intermittent, cleared on restart, and recurred.
Root cause
The hang is in the OAuth token-refresh path, not the network.
googleapiclient's_auth.refresh_credentials()builds a barehttplib2.Http()with no timeout, andmain()sets no process-wide default socket timeout. httplib2 withtimeout=Nonenever callssettimeout(), so the refresh POST runs on a fully unbounded socket. Normally it's sub-second, but when a socket stalls (stale keep-alive reuse / transient TLS–TCP hiccup) it blocks forever instead of erroring.The server already sets
httplib2.Http(timeout=30)in_build_authorized_http(), but that doesn't cover the library's internal refresh path — hence the gap.Fix
Set a process-wide default socket timeout at the top of
main():A raw socket inherits
socket.getdefaulttimeout()at creation, so the timeout-less refreshhttplib2.Http()now gets a ceiling and fails fast (and retries) instead of hanging.asyncio/uvicornsockets are set non-blocking and ignore the default, so the streamable-HTTP transport is unaffected. The value (30s) matches the existing_build_authorized_http()convention.socketis already imported inmain.py, so this is a one-line change plus a comment.Verification
After applying, ran 7 back-to-back Gmail calls (searches + a content-batch fetch) with no drops and no refresh hang; the previously-hanging
Attempting refresh…now completes.Summary by CodeRabbit