Skip to content

bug: LLM HTTP client has no configurable request timeout, causing scan to hang indefinitely #237

Closed

Description

@zy84338719

Bug Description

The ocr scan command can hang indefinitely when the LLM API does not respond. The HTTP client used for LLM API calls does not have a properly configurable request timeout, causing the process to block forever on stuck connections.

Root Cause

In internal/llm/client.go, the NewLLMClient factory function creates a ClientConfig without setting the Timeout field:

func NewLLMClient(ep ResolvedEndpoint) LLMClient {
    cfg := ClientConfig{
        URL:        ep.URL,
        APIKey:     ep.Token,
        Model:      ep.Model,
        AuthHeader: ep.AuthHeader,
        ExtraBody:  ep.ExtraBody,
        // Timeout is NOT set — defaults to 0
    }

While NewOpenAIClient and NewAnthropicClient both have a fallback default of 5 minutes:

if cfg.Timeout <= 0 {
    cfg.Timeout = 5 * time.Minute
}

There are two problems:

  1. ResolvedEndpoint has no Timeout field — users cannot configure the HTTP timeout via config file or environment variables
  2. Streaming responses may bypass the timeout — the OpenAI/Anthropic SDKs WithRequestTimeout applies to the overall request lifecycle, but streaming responses that send data slowly (or stop sending data mid-stream) may not trigger the timeout

Reproduction

  1. Configure ocr with an LLM API that occasionally hangs (e.g., rate-limited or overloaded)
  2. Run ocr scan on a repository
  3. The process hangs indefinitely with 0% CPU usage, holding an ESTABLISHED TCP connection to the API server
  4. The process never exits until manually killed

Expected Behavior

The ocr binary should:

  1. Support a configurable HTTP request timeout (e.g., via OCR_LLM_TIMEOUT env var or config file)
  2. Have a reasonable default timeout (e.g., 120 seconds per LLM call)
  3. Properly enforce the timeout on streaming responses

Environment

  • ocr version: v1.6.4 (darwin/arm64)
  • OS: macOS
  • LLM Provider: Xiaomi MiMo API (OpenAI-compatible)

Suggested Fix

  1. Add a Timeout field to ResolvedEndpoint
  2. Add OCR_LLM_TIMEOUT environment variable support
  3. Pass the timeout from ResolvedEndpoint to ClientConfig in NewLLMClient
  4. Add per-request timeout enforcement that works with streaming responses

Activity

  1. added a commit that references this issue on Jun 30, 2026
    eae8635
  2. added a commit that references this issue on Sep 17, 2026
    976e26f
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