Skip to content

desktop-backend: Gemini proxy generateContent requests time out with 504/502 #8633

Closed

Description

@Git-on-my-level

Summary

Cloud Run request logs show a recurring desktop-backend failure cluster where Gemini proxy calls to gemini-2.5-flash:generateContent hit 504s at roughly the 300s Cloud Run boundary, plus some 502s. This appears in both prod and dev. Users likely see stuck/failed desktop AI/chat operations when these requests time out.

No existing issue found for desktop-backend Gemini 504 / gemini-2.5-flash:generateContent timeouts. Related closed issue #7624 added UID logging for successful Gemini proxy requests, but this is specifically about timeout/failure handling.

Evidence

Sanitized observability sweep run: 2026-06-29 UTC.

Cloud Logging query shape: Cloud Run severity>=ERROR, 24h window, sanitized/bucketed by status + method + path. Request bodies, user IDs, IPs, and tokens are intentionally omitted.

Prod: project based-hardware, service desktop-backend

Status Endpoint Sampled count
504 POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent 102
502 POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent 13

Representative sanitized shape:

  • latency around 300.0s for many 504s
  • request sizes observed in the hundreds of KB in the Cloud Run metadata
  • response size small, indicating error response rather than successful model output

Dev: project based-hardware-dev, service desktop-backend

Status Endpoint Sampled count
504 POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent 192
502 POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent 4

Related product analytics

PostHog macOS, latest 24h:

Event Events Users
chat_agent_error 124 49
chat_agent_query_completed 406 59
chat_tool_call_completed 2,074 52

Top chat_agent_error messages included Response stopped., AI not available: bridge failed to start, Something went wrong. Please try again., and AI service is temporarily unavailable. Please try again later. These are not proven one-to-one with the Gemini timeout bucket, but they are consistent with desktop AI reliability issues in the same window.

Suggested investigation / fix direction

  1. Inspect desktop-backend Gemini proxy route for timeout policy, request-size limits, retry behavior, and whether the client waits synchronously for the full model response.
  2. Add structured logging around upstream Gemini duration/result buckets without logging prompt/body content.
  3. Decide whether requests near the Cloud Run deadline should be rejected earlier with a clear retryable error, streamed, chunked, or moved to an async/job path.
  4. Check whether very large request payloads correlate with the 300s timeouts and whether the desktop client can trim context or cap payload size.
  5. Ensure desktop clients show a specific recoverable message for proxy timeout instead of a generic bridge failure.

Acceptance criteria

  • 504s on POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent drop materially in prod and dev Cloud Run logs.
  • The proxy has explicit upstream timeout/error mapping and does not simply hit the Cloud Run request deadline.
  • Large/slow requests are handled with a clear user-facing retry/cancel path.
  • Add regression coverage for timeout/error mapping in desktop-backend, using sanitized fake payloads.

Related existing issues checked

Activity

  1. added
    bug
    chat
    backend
    intelligence
    p1
    on Jun 29, 2026
  2. Git-on-my-level commented on Jun 29, 2026

    @Git-on-my-level
    MemberAuthor

    Additional code pointers from the repo:

    Client side:

    • desktop/macos/Desktop/Sources/ProactiveAssistants/Core/GeminiClient.swift:323-325 builds proxy URLs as v1/proxy/gemini/models/<model>:<action>.
    • GeminiClient.swift:424-430 sends image/structured requests to generateContent with urlRequest.timeoutInterval = 300.
    • GeminiClient.swift:498-504 sends text requests to generateContent with a default timeout of 300 seconds.
    • GeminiClient.swift:350-359 maps non-2xx responses to GeminiClientError.apiError("HTTP <status>: <body>").

    Server side:

    • desktop/macos/Backend-Rust/src/routes/proxy.rs:77-87 defines POST /v1/proxy/gemini/*path.
    • proxy.rs:111-116 sanitizes/caps the Gemini body.
    • proxy.rs:149-157 forwards non-BYOK requests via gemini_proxy_server_key.
    • proxy.rs:175-201 handles BYOK Gemini proxying.
    • proxy.rs:236-310 sends the upstream Vertex/AI Studio request using fresh reqwest::Client::new() calls with no obvious per-upstream timeout in the call path.
    • proxy.rs:312-315 maps upstream request failures to 502 Bad Gateway.
    • proxy.rs:317-339 passes through upstream status/body.

    Why this matters for the 504 evidence: many prod/dev Cloud Run request logs were exactly at the ~300s boundary. The desktop client and Cloud Run request deadline are both effectively waiting for a full response. A likely fix is to add explicit upstream timeout/error mapping before Cloud Run kills the request, plus client UX that treats this as retryable/cancellable. Streaming (/v1/proxy/gemini-stream/*path) may also be relevant for long-running calls.

    No prompt/request body content was included in the issue evidence. Please keep any new logs to sanitized fields such as model, action, payload byte size bucket, upstream provider, duration bucket, status, and timeout reason.

  3. added a commit that references this issue on Jun 29, 2026
    8a435b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendbugchatdesktopintelligencemacOSp1reliability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions