Skip to content

antigravity: disable Bun fixed 300s fetch timeout on streamGenerateContent and enforce explicit first-chunk / stream-idle deadlines #1318

Open

Description

@Ziqi-Cui

Package

@magpie-community/opencode-antigravity-auth (0.2.3, packages/antigravity/index.mjs)

Problem

In complete() (send() at packages/antigravity/index.mjs line 1595) and webSearch() (line 2212), fetch("${DAILY}/v1internal:streamGenerateContent?alt=sse", ...) is called without timeout: false. Under Magpie's Bun plugin host (Bun 1.4.4), fetch applies Bun's built-in ~300-second socket timeout (TimeoutError, code 23, The operation timed out.).

Relying on Bun's fixed ~300s socket deadline creates two operational issues on Gemini 4 streaming requests.

  1. Silent upstream stalls take 5+ minutes per attempt and stack with retries. On gemini-4-carbon, Google's v1internal:streamGenerateContent endpoint occasionally accepts the request payload and stalls without emitting response headers or SSE chunks (similar upstream behavior is tracked in google-antigravity/antigravity-cli#1402 and #1546). Each stalled fetch blocks for ~300–330 seconds before throwing TimeoutError. Because no SSE event reached the gateway (ttft remains empty), Magpie records 502 and retries after 1 second, stretching a single upstream stall to 9–10 minutes. If complete() also performs internal RETRY_BAD_CALL retries (holdThoughts / badCall loop at lines 1639–1661) before a subsequent attempt stalls, the earlier attempts stay hidden from the gateway and extend the wait even further.

  2. Pre-first-chunk waiting and active SSE streaming share one fixed runtime timer. Without timeout: false and explicit stream-stage timers, slow dead connections cannot fail earlier than ~300s while healthy long-running streams lack dedicated per-chunk activity tracking.

Measured Behavior

Across a day of live gemini-4-carbon traffic through Magpie (Bun 1.4.4) up to 292k input tokens, we observed a clear gap between normal single-ask latency and silent stalls.

  • 74 of 75 successful gemini-4-carbon turns delivered their first gateway event within 180.4s (most complex turns landed between 70s and 180s). The single 338.8s outlier (80 reasoning tokens) came from an internal two-ask INVALID_ARGUMENT retry inside complete().
  • Stalled gemini-4-carbon attempts consistently hit Bun's default timeout at 312.3s, 327.0s, and 330.0s with zero TTFT (502).
  • On one stalled turn (327.0s on the first attempt), Magpie's automatic retry succeeded in 28.5s once a fresh upstream request was opened. On another stalled turn (312.3s on the first attempt), the second attempt also stalled until the caller cancelled at 556.6s (9m17s total).
  • Reproducing the silent server locally on Magpie's bundled Bun 1.4.4 confirmed that reading the request body and withholding headers or SSE bytes produces the same TimeoutError (code: 23) and triggers Magpie's 1-second 502 retry.

Proposed Improvement

  1. Pass timeout: false in the fetch init inside send() (packages/antigravity/index.mjs line 1595) so Bun's fixed ~300s socket timer is disabled in favor of explicit stage deadlines.
  2. Wrap the v1internal:streamGenerateContent call and its response ReadableStream with two separate AbortController timers linked to init?.signal
    • First-chunk deadline (for example 210s default) armed until the first non-empty SSE chunk arrives from upstream, then cleared.
    • Stream-idle deadline (for example 180s default) reset on each reader.read() pull after the first chunk arrives so active long streams can run arbitrarily long while mid-stream stalls still abort cleanly.
  3. Propagate caller cancellation on init?.signal immediately and clear all timers on stream completion, cancellation, or error.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions