Skip to content

fix: add socket timeout so a hung OAuth token refresh can't stall the server - #911

Open
ModiZ613 wants to merge 1 commit into
taylorwilsdon:mainfrom
ModiZ613:fix/oauth-refresh-timeout
Open

fix: add socket timeout so a hung OAuth token refresh can't stall the server#911
ModiZ613 wants to merge 1 commit into
taylorwilsdon:mainfrom
ModiZ613:fix/oauth-refresh-timeout

Conversation

@ModiZ613

@ModiZ613 ModiZ613 commented Jul 8, 2026

Copy link
Copy Markdown

Problem

Running in service-account / domain-wide-delegation mode, we hit intermittent Session terminated errors reaching clients. Server logs showed the Google API call succeeding, immediately followed by:

[INFO] Attempting refresh to obtain initial access_token

…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 bare httplib2.Http() with no timeout, and main() sets no process-wide default socket timeout. httplib2 with timeout=None never calls settimeout(), 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():

socket.setdefaulttimeout(30)

A raw socket inherits socket.getdefaulttimeout() at creation, so the timeout-less refresh httplib2.Http() now gets a ceiling and fails fast (and retries) instead of hanging. asyncio/uvicorn sockets 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.

socket is already imported in main.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

  • Bug Fixes
    • Improved startup reliability by setting a default timeout for new blocking network connections.
    • Reduced the chance of the app hanging indefinitely on stalled HTTP or socket requests, especially during sign-in and refresh flows.

… 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>
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 72344701-8473-4c97-b6e4-9f63ad86a5a2

📥 Commits

Reviewing files that changed from the base of the PR and between 3f19c7d and acf294c.

📒 Files selected for processing (1)
  • main.py

📝 Walkthrough

Walkthrough

This PR adds a single line in main.py that sets a process-wide default socket timeout of 30 seconds early in main(), bounding otherwise-unbounded blocking socket operations such as those used during OAuth token refresh.

Changes

Default Socket Timeout

Layer / File(s) Summary
Startup socket timeout
main.py
main() now calls socket.setdefaulttimeout(30) early during startup, with comments explaining it prevents stalled OAuth refresh calls from hanging indefinitely.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, root cause, fix, and verification, but it does not follow the repository's required template sections. Reformat the PR description to include the required Description, Type of Change, Testing, Checklist, and Additional Notes sections, including the maintainers-edit checkbox.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: adding a socket timeout to prevent hung OAuth refreshes from stalling the server.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@taylorwilsdon taylorwilsdon self-assigned this Jul 16, 2026
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.

2 participants