Skip to content

Backend routing breaks after proxy runner restart with backendReplicas > 1 #4575

Closed

Description

@jerm-dro

Problem

When an MCPServer has backendReplicas > 1, the proxy runner stores the ClusterIP service URL (e.g. http://mcp-<name>:8080) as backend_url in session metadata. After a proxy runner restart, it recovers the session from Redis but routes via ClusterIP — kube-proxy may send the request to a backend pod that never handled the initialize for that session.

The backend pod returns HTTP 404 with JSON-RPC -32001 ("session not found") because it has no record of that session. The mcp-go client surfaces this as:

transport error: failed to send request: session terminated (404). need to re-initialize

The session is not actually terminated — it simply doesn't exist on the pod that received the request.

Parent issue: #4484

Failing acceptance test

PR #4574 adds an E2E test that demonstrates this bug. It fails on all 3 Kubernetes versions in CI:

Error from CI (v1.35.1):

[FAILED] Request 1/5 should succeed — session should route to the correct backend
Unexpected error:
    transport error: failed to send request: session terminated (404). need to re-initialize
In [It] at: mcpserver_scaling_test.go:382

Test flow

  1. Deploy MCPServer with replicas=1, backendReplicas=2, Redis session storage, sessionAffinity=None
  2. Initialize MCP session, call tools/list — succeeds
  3. Delete the proxy runner pod (Deployment recreates it)
  4. Send 5 tools/list requests with the same session ID — fails on request 1

With 2 backends and random routing, P(all 5 hit correct pod) ≈ 3%.

Root cause

  1. StatefulSet serviceName mismatch: buildStatefulSetSpec sets serviceName to containerName, but the headless service is named mcp-<containerName>-headless. These must match for Kubernetes to create pod DNS records.

  2. ClusterIP stored as backend_url: RoundTrip stores targetURI (ClusterIP) as backend_url. After restart, Rewrite routes via ClusterIP with sessionAffinity=None, hitting a random backend.

Proposed fix

Store a pod-specific headless DNS URL (e.g. myserver-0.mcp-myserver-headless.default.svc.cluster.local:8080) as backend_url instead of the ClusterIP:

  • Fix StatefulSet serviceName to match headless service name
  • Add HeadlessServiceConfig to ScalingConfig
  • Pre-select a random StatefulSet pod on initialize, store its headless DNS as backend_url
  • Operator populates config when backendReplicas > 1
  • Backward compatible: when config is nil (single backend), behavior is unchanged

Activity

  1. added
    bug
    on Apr 6, 2026
  2. added
    go
    kubernetes
    scalability
    on Apr 6, 2026
  3. self-assigned this
    on Apr 7, 2026
  4. added 5 commits that reference this issue on Apr 7, 2026
    7ffc605
    8f31e6c
    a2c1cf3
    3b743b9
    64c5b0f
  5. JAORMX commented on Apr 8, 2026

    @JAORMX
    Collaborator

    Heads up: while reviewing #4638, we found some fundamental issues with the headless service it builds on top of (wrong selector target, StatefulSet serviceName mismatch, random pod selection after initialize). Opened #4668 to track rethinking the headless service before we add more plumbing on top.

  6. yrobla commented on Apr 8, 2026

    @yrobla
    Contributor

    Proposed fix: per-pod ClusterIP services

    After reviewing the current approach (headless DNS routing) with the team, we identified a security concern: storing pod-specific headless DNS URLs in Redis leaks internal cluster topology and allows bypassing the proxy's auth middleware from within the cluster. Since the headless service is also going to be removed, here's the proposed replacement.

    Approach

    The operator creates one ClusterIP service per backend pod ordinal alongside the existing shared service:

    mcp-<name>        → selects all backend pods (existing, unchanged)
    mcp-<name>-pod-0  → selects only pod <name>-0  (new)
    mcp-<name>-pod-1  → selects only pod <name>-1  (new)
    

    Each per-pod service uses the label Kubernetes automatically assigns to StatefulSet pods:

    selector:
      statefulset.kubernetes.io/pod-name: <name>-<ordinal>

    The proxy runner computes the ordinal deterministically from the session ID:

    ordinal = fnv32(sessionID) % backendReplicas
    backend = "http://mcp-<name>-pod-{ordinal}:<port>"
    

    Why this is better

    • No topology leakage: nothing about pod addressing is stored in Redis — routing is computed on every request from the session ID alone
    • No bypass risk: ClusterIP services route through kube-proxy, not raw pod IPs; the proxy's auth middleware cannot be circumvented
    • Survives proxy restart: any proxy pod computes the same ordinal for a given session ID — no state recovery needed
    • Standard Kubernetes pattern: per-pod services via statefulset.kubernetes.io/pod-name selector are well-supported and widely used
    • Operator manages lifecycle: services are created/deleted as backendReplicas scales, same as today's shared service

    Changes required

    1. Operator (): add createPerPodServices that creates one ClusterIP service per ordinal up to backendReplicas; delete excess services on scale-down
    2. Runner config: replace HeadlessServiceConfig with a BackendServiceConfig carrying the base service name (mcp-<name>-pod) and replica count
    3. Proxy (): on each initialize request, compute fnv32(sessionID) % replicas and store nothing — just forward to the right per-pod service
  7. JAORMX commented on Apr 8, 2026

    @JAORMX
    Collaborator

    So, I've been looking at this issue and the proposed fixes, and I think we need to take a step back here.

    Both proposals (headless DNS routing and per-pod ClusterIP services) share a fundamental problem: they create infrastructure that exposes backend pods outside the proxy runner. The headless DNS approach puts pod-specific addresses in Redis and makes them resolvable from anywhere in the cluster. The per-pod services create addressable endpoints that bypass the proxy's auth/authz/audit middleware entirely. For stdio servers this is a non-starter since the proxy runner IS the only HTTP endpoint... but even for SSE/streamable-http, we should never bypass the proxy runner. That's the whole point of the architecture.

    Also, the fnv32(sessionID) % replicas idea doesn't actually work for the initialize flow. At initialize time there IS no session ID yet. The backend creates it. Per the MCP spec (both 2025-03-26 and 2025-11-25):

    A server using the Streamable HTTP transport MAY assign a session ID at initialization time, by including it in an Mcp-Session-Id header on the HTTP response containing the InitializeResult.

    And:

    ...it MUST start a new session by sending a new InitializeRequest without a session ID attached.

    So you can't deterministically route the initialize to a specific pod based on the session ID. It doesn't exist yet.

    Note that the sticky session work we already landed (ClientIP affinity on the backend service via #3992, Redis session storage via #4525) covers most of this. The gap is specifically: when the proxy runner restarts, it gets a new pod IP, so the L4 ClientIP affinity mapping to the backend is lost. The session is in Redis, but backend_url points to the ClusterIP service URL, which doesn't help route to the right pod.

    The fix should be way simpler than what's proposed here. The backend_url session metadata mechanism already works perfectly... it just stores the wrong value. Go's net/http/httptrace package gives us a GotConn callback that fires after kube-proxy DNAT. info.Conn.RemoteAddr() returns the actual pod IP that handled the connection. So:

    1. On initialize, attach an httptrace.ClientTrace with GotConn to capture the resolved pod IP
    2. Store http://<pod-ip>:<port> as backend_url instead of the ClusterIP URL
    3. That's it. The existing Rewrite function already reads backend_url and overrides the outbound URL

    All traffic still flows through the proxy runner. No new Kubernetes resources. No new bypass vectors. ~30 lines of code in transparent_proxy.go.

    "But what if the backend pod restarts and gets a new IP?" Well... the backend's in-memory MCP session state is gone too. The client has to re-initialize regardless. The proxy should handle the stale IP gracefully (connection refused or 404 → clear session from Redis → client re-initializes → new pod IP captured). This isn't a new failure mode.

    For environments with frequent backend pod churn, we can add an EndpointSlice watcher as a follow-up that maps pod IPs to stable StatefulSet ordinals. But that's an optimization, not the fix.

  8. yrobla commented on Apr 8, 2026

    @yrobla
    Contributor

    Reviewed the httptrace approach — it solves the problem cleanly. The proxy runner stays fully in the path: pod IPs are only used on the proxy's outbound leg (inside the Rewrite closure), never visible to clients.

    One thing worth adding on top: when the outbound connection to a stored pod IP fails (connection refused, reset, etc.), the proxy should clear backend_url from the session — or delete the session entirely — so the client receives a clean 404/503 and knows to reinitialize, rather than retrying against a stale address. Without this, a backend pod restart leaves the session in a permanently broken state in Redis until TTL expiry.

  9. yrobla commented on Apr 8, 2026

    @yrobla
    Contributor

    in the case of backend restarting, it will loose the ip and get a new one. In this case we do not have to return 404 , but instead reinitialize and create a new session

  10. yrobla commented on Apr 8, 2026

    @yrobla
    Contributor

    Final implementation decision

    After the discussion above, we are going with JAORMX's httptrace approach as the foundation. This section captures the full implementation plan that will be used.


    Core mechanism — GotConn pod IP capture

    Use Go's net/http/httptrace package to intercept the resolved pod IP after kube-proxy DNAT completes:

    1. On every initialize request, attach an httptrace.ClientTrace with a GotConn callback to the outbound request context.
    2. Inside GotConn, read info.Conn.RemoteAddr() — this is the actual backend pod IP kube-proxy resolved to.
    3. Store http://<pod-ip>:<port> as backend_url in Redis for that session.
    4. The existing Rewrite function already reads backend_url and overrides the outbound URL — no changes needed there.

    All traffic continues to flow through the proxy runner. No new Kubernetes resources, no new bypass vectors. ~30 lines of code in transparent_proxy.go.


    Backend restart handling — transparent re-initialization instead of 404

    This is the key addition on top of JAORMX's approach.

    When the proxy attempts to forward a request to a stored pod IP and the connection fails (connection refused, connection reset, TCP timeout), it means the backend pod has been rescheduled and now holds a different IP. In this case:

    • Do NOT return 404 to the client. The session is not the client's fault and the pod is likely already healthy on a new IP.
    • The proxy detects the stale connection (dial error or first-byte reset against the stored backend_url).
    • It clears backend_url from the session in Redis, then issues a fresh initialize request against the ClusterIP service (no pod IP — kube-proxy picks a healthy pod).
    • The GotConn callback fires again, capturing the new pod IP, which is stored back into Redis as the updated backend_url.
    • The proxy then replays the original client request against the newly-initialized backend session and returns the response normally.
    • The client sees no error. The session ID it holds continues to work.

    This handles the primary failure mode: proxy runner restart causes backend_url to still be valid in Redis, but the backend pod that originally served the session has since been replaced.


    Changes required

    pkg/proxy/transparent_proxy.go (or equivalent transport layer)

    • RoundTrip: on initialize, wrap the request context with httptrace.NewClientTrace / context.WithValue; in the GotConn callback capture info.Conn.RemoteAddr().String() and pass it out (via a pointer or channel) to be stored as backend_url.
    • Rewrite (or equivalent pre-send hook): detect dial/connection errors against the stored backend_url; on failure, clear backend_url from Redis and trigger re-initialization as described above before retrying once.

    Session storage (Redis)

    • No schema changes. backend_url already exists and is already used. This PR only changes what value is written to it and adds the stale-detection + clear logic.

    Operator / Kubernetes resources

    • No changes required. Per-pod services, headless services, and HeadlessServiceConfig / BackendServiceConfig structs are all dropped. The operator continues to create only the shared ClusterIP service, as today.

    Acceptance criteria

    • After a proxy runner pod restart, the first tools/list request with an existing session ID succeeds without the client needing to reinitialize.
    • With backendReplicas=2 and sessionAffinity=None, all requests for a given session ID consistently reach the same backend pod (the one that handled initialize).
    • When the backend pod that owned a session is deleted and rescheduled (new pod IP), the proxy transparently reinitializes, updates Redis, and continues serving the client — no 404 is returned.
    • Single-backend deployments (backendReplicas=1) are unaffected — behavior is unchanged.
    • The E2E test added in PR Add E2E acceptance test for backend routing after proxy restart #4574 passes on all three Kubernetes versions.
    • No new Kubernetes resources (per-pod services, headless services) are created by the operator.
    • Pod IPs are never surfaced to clients or stored in a way that is externally visible — they are only used on the proxy's outbound leg.

    What is explicitly out of scope

    • EndpointSlice watcher / pod-IP-to-ordinal mapping: useful optimization for environments with very high backend pod churn, tracked as a follow-up.
    • Per-pod ClusterIP services (mcp-<name>-pod-N): rejected due to bypass risk.
    • Headless DNS routing: rejected due to topology leakage and the StatefulSet serviceName issues identified in Remove or rethink the MCP backend headless service #4668.

    This is the approach we will implement. Closing the headless service track (#4668) is a prerequisite to avoid conflicting work.

  11. added 3 commits that reference this issue on Apr 8, 2026
    6b5cb78
    cac1257
    32960b1
  12. added a commit that references this issue on Apr 9, 2026
    d851c69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buggokubernetesscalability

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions