Skip to content

bug: Forward proxy ends a broken upstream stream as a clean, complete 200 #1543

Closed

Description

@huang195

What happens

When the upstream breaks a streamed response partway, the forward proxy ends the client's response cleanly. The agent sees a complete 200 with no error and treats a cut-off answer as finished. The session row says 200 with no error either, so agentop hides the failure too.

The same happens when the proxy's own 5-minute idle limit (streamReadIdleTimeout, server.go:91) cuts a stream.

Reproduction

The upstream sends one SSE event, then resets the TCP connection mid-body:

direct:                                   status=200 readErr=read tcp ...: connection reset by peer
proxied, no plugins (passthrough copy):   status=200 readErr=<nil>
proxied, streaming responder (SSE relay): status=200 readErr=<nil>

The same happens over the TLS bridge, on both h1 and h2: curl exits 18 (partial transfer) direct and 0 through the proxy.

Test source (drop into core/listener/forwardproxy/)
package forwardproxy

import (
	"io"
	"net"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/rossoctl/cortex/core/pipeline"
)

func abortingSSE(w http.ResponseWriter, _ *http.Request) {
	w.Header().Set("Content-Type", "text/event-stream")
	w.WriteHeader(http.StatusOK)
	_, _ = io.WriteString(w, "event: a\ndata: {\"n\":1}\n\n")
	w.(http.Flusher).Flush()
	conn, _, _ := w.(http.Hijacker).Hijack()
	_ = conn.(*net.TCPConn).SetLinger(0)
	_ = conn.Close()
}

func TestTruncatedStreamReachesClientAsError(t *testing.T) {
	up := httptest.NewServer(http.HandlerFunc(abortingSSE))
	defer up.Close()
	for _, plugins := range [][]pipeline.Plugin{nil, {newStreamingProbe(false)}} {
		pipe, _ := pipeline.New(plugins)
		srv, _ := NewServer(pipeline.NewHolder(pipe), nil, nil)
		proxy := httptest.NewServer(srv.Handler())
		client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(mustParseURL(proxy.URL))}}
		resp, err := client.Get(up.URL)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := io.ReadAll(resp.Body); err == nil {
			t.Errorf("plugins=%d: truncated upstream stream reached the client as a clean end of body", len(plugins))
		}
		resp.Body.Close()
		proxy.Close()
	}
}

Why

On a read error, all three relay paths stop copying and return normally:

  • handleStreamingResponse, core/listener/forwardproxy/server.go:1827: slog.Warn(...); break
  • streamPassthrough, server.go:1950: slog.Warn(...); return
  • the unbuffered io.Copy, server.go:747: logs at Debug, then records the row and returns

Once the handler returns normally, net/http writes the terminating zero-length chunk (h1) or END_STREAM (h2). To the client that means "the body is complete". The status was already sent, so the only way left to signal failure is to abort the connection.

What this means in the laptop's log

Over 17 hours, proxy.log held 103 streaming response read error lines, and each one reached the agent as a normal end of stream:

  • 68 are on mcp.ete-server. They are the proxy's own idle cutoff killing MCP's long-lived $transport/stream GET: the rows show durationMs 300044 / 300048, and the errors read use of closed network connection / read on closed response body, which is what idleReader's own close produces.
  • 24 are context canceled: the client hung up first. Those are logged at WARN, though CLAUDE.md says a client cancel "logs no WARN".
  • 18 are unexpected EOF.
  • 8 are connection reset by peer.

Possible fix

After the Warn, panic(http.ErrAbortHandler) instead of break/return. That is the standard library's own way to abort a response:

  • the deferred recordOutboundResponseEvent and finalize still run;
  • net/http resets the connection (h1) or sends RST_STREAM (h2) instead of a clean end;
  • ErrAbortHandler is not logged as a panic.

Also put the read error on the response row (error.kind: upstream_error, as the buffered path does for #1045), so the session view stops showing these as clean 200s.

Two things to decide while fixing:

  • The client-canceled case (context canceled) has no one left to abort for. It should stay a plain return, and probably drop to Debug.
  • The idle cutoff on MCP's standing GET is a separate question: whether 5 minutes is right for a stream that is idle by design. That is out of scope here. With this fix the cut is at least visible to the client, which can then reconnect.

Acceptance

  • The test above passes, for both the passthrough and the SSE-relay arms, and for a bridged (TLS) request.
  • The response row for a truncated stream carries an error.
  • Existing sse_relay_test.go, mcp_sse_*_test.go and bytescount_test.go pass.

Found by an experiment-driven audit of main at 43cf553; every number above was re-measured on that commit.

Assisted-By: Claude (Anthropic AI) [email protected]

Activity

  1. added
    bug
    on Oct 29, 2026
  2. huang195 commented on Oct 29, 2026

    @huang195
    MemberAuthor

    CI test for this bug class: #1552. A table test across every response path. Measured: the reverse proxy already reports the break correctly; only the forward proxy hides it.

  3. self-assigned this
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions