Skip to content

mcpx(daemon): chat-completions proxy has no upstream timeout and buffers the whole upstream response in memory — proxyToUpstream uses http.DefaultClient and an uncapped TeeReader buffer #2114

Closed

Description

@dezren39

Problem

The mcpx daemon's chat-completions reverse proxy (proxyToUpstream, pkgs/mcpx/internal/daemon/routes_proxy.go:215) has no bound on either direction of its upstream call:

1. No upstream timeout. routes_proxy.go:237 calls http.DefaultClient.Do(upReq). http.DefaultClient has no Timeout, and the client sets no ResponseHeaderTimeout — the only cancellation is the inbound request's context, i.e. the downstream client disconnecting. The daemon's own HTTP servers set only ReadHeaderTimeout (internal/daemon/server.go:309,361) — no write/response deadline rescues a stuck handler. An upstream that accepts the connection and never answers (or trickles forever) holds the handler, its goroutine and the downstream connection indefinitely. This is reachable per-request: handleChatCompletions takes the upstream URL from the X-Upstream-Url request header when present (routes_proxy.go, step 4, ~:150), so any local client can aim the daemon at a slow-loris endpoint.

2. The whole upstream response is buffered in memory, uncapped. routes_proxy.go:245-262: the response body is streamed through an io.TeeReader into a bytes.Buffer, then buf.String() is stored as the feedback interaction record after io.Copy finishes. The copy itself streams, but the buffer defeats it — memory grows with the full response size, with no cap. A streamed request makes this worst-case: chatCompletionReq.Stream (routes_proxy.go:38) is marshalled straight into the proxied payload (:227) and handleChatCompletions proxies unconditionally (:164), so a long SSE stream accumulates in the buffer for its entire life.

Verified on main 42796be5bbe8824545df570f3b239d23d4ef93ac by reading the code this run.

The house pattern this misses

Every other outbound HTTP call in mcpx pins a timeout or a body bound:

  • internal/catalog/embeddings.go:52 — &http.Client{Timeout: defaults.EmbeddingsRemoteTimeout}
  • internal/mcpclient/sse.go:58 — &http.Client{Timeout: timeout}; error bodies read via io.LimitReader(..., defaults.HTTPErrorBodyLimit) (sse.go:76,225)
  • internal/cli/client.go:118-162 — all daemon clients built with explicit timeouts
  • internal/daemon/ops.go:285 — registry calls use cs.Duration("registry.timeout")

routes_proxy.go:237 is the only http.DefaultClient use in internal/daemon.

Why it matters

The daemon is the process every other tool call depends on. One hung upstream (a crashed provider gateway, a misconfigured proxy.upstreamUrl, a header-aimed slow endpoint) leaks a handler + connection pair forever per request, and one large or streamed response can grow daemon memory without limit — the daemon-side mirror of the inbound body-cap gap in #2082 (request bodies), on the response path #2082 does not cover. Distinct from #2097/#2100/#2102: those are the standalone proxy packages; this is mcpx's own daemon proxy route.

Acceptance criteria

  • proxyToUpstream uses a client with a configured timeout (a proxy.* setting with a sane default, in the style of registry.timeout / defaults.EmbeddingsRemoteTimeout), covering at least time-to-headers and total response time; a timed-out upstream yields a clean 502 to the downstream client.
  • The feedback-record copy of the upstream response is capped (e.g. io.LimitReader at a named constant, in the spirit of defaults.HTTPErrorBodyLimit); over-cap responses are still proxied in full to the client, only the recorded copy is truncated, and the truncation is visible in the record.
  • Tests: a stub upstream that accepts and never responds makes the handler return within the configured timeout; a stub upstream returning an over-cap (or streamed) response is proxied completely while daemon-side buffering stays bounded.
  • Existing daemon test suites pass unchanged.

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/small-fixQueue packet kind: bounded fix with reproduction and targeted tests
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    on Oct 29, 2026
  2. developing-today commented on Oct 29, 2026

    @developing-today
    Contributor
  3. added
    app/mcpxPackage/app: mcpx (pkgs/ or apps/ in this repo)
    on Oct 29, 2026
  4. dezren39 commented on Oct 29, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

  5. added
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    and removed on Nov 1, 2026
  6. 55 remaining items

  7. added and removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 3, 2026
  8. dezren39 commented on Nov 3, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · parked

  9. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    and removed
    queue/claimedQueue worker lifecycle state
    on Nov 3, 2026
  10. added a commit that references this issue on Nov 3, 2026
  11. dezren39 commented on Nov 3, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

  12. removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    work/assignedQueue worker lifecycle state
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/mcpxPackage/app: mcpx (pkgs/ or apps/ in this repo)kind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobsource/design-architectFiled by the repo design architect jobtask/small-fixQueue packet kind: bounded fix with reproduction and targeted tests

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions