Skip to content

Re-initialize a replaced stdio backend after re-attach - #7296

Draft
somaz94 wants to merge 1 commit into
stacklok:mainfrom
somaz94:fix/stdio-reinit-on-reattach
Draft

somaz94 wants to merge 1 commit into
stacklok:mainfrom
somaz94:fix/stdio-reinit-on-reattach

Conversation

@somaz94

@somaz94 somaz94 commented Nov 25, 2026

Copy link
Copy Markdown

Summary

When the operator replaces the <name>-0 pod of a stdio MCPServer, the proxy re-attaches to the new process in place but keeps answering every client initialize from the result it cached for the old one. The new process never receives initialize, so a server that enforces the MCP lifecycle (anything on the Go SDK) rejects every later request with method "tools/call" is invalid during session initialization until someone deletes the proxy pod.

After a successful re-attach, StdioTransport now asks the streamable proxy to replay the cached handshake. HTTPProxy.ReinitializeBackend sends the stored initialize params to the backend inside the same singleflight as client handshakes, and on success replaces the cached result and sends notifications/initialized. A JSON-RPC error is read as "this process is already initialized", which is what a strict server answers when only the attach stream dropped, so the cache is kept. A replay that gets no answer clears the cache, so the next client handshake goes to the backend. docs/arch/03-transport-architecture.md describes the re-attach behavior.

This addresses mode 2 of #6974 only. Mode 1, a restarted proxy whose first initialize reaches a backend that is still initialized, stays open, since it needs a decision between restarting the backend pod and another recovery.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

task lint-fix reported 0 issues. task test passed apart from TestEnvoyBootstrap_ValidatesAgainstRealEnvoy and TestEgressSquidIPv6ConfigValidatesAgainstRealSquid in pkg/container/docker, which need a running Docker daemon that this machine did not have. TestReinitializeBackend covers the four outcomes of the replay against a fake strict backend and fails on three of them without the change; it and the re-attachment tests in pkg/transport passed ten runs in a row with -race. I have not reproduced the fix on a cluster.

Does this introduce a user-facing change?

Yes. Before, after the backend pod of a stdio MCPServer was replaced, a client's initialize succeeded from the cache but every request after it failed on a strict server until the proxy pod was restarted. After, the proxy initializes the new process itself when it re-attaches, so requests keep working.

Special notes for reviewers

Reading any JSON-RPC error as "already initialized" means a new process that rejects initialize for some other reason goes undetected, as it does today. A request that reaches the new process between the re-attach and the end of the replay can still fail. ReinitializeBackend retries up to three times when it joins a flight another caller started, because that flight finishes without replaying.

Generated with Claude Code

When the backend pod behind a stdio proxy is replaced, the transport
re-attaches to the new process, but the proxy keeps answering client
handshakes from the initialize it cached for the old one. The new
process never sees an initialize, so servers that enforce the MCP
lifecycle reject every later request.

After a successful re-attach the proxy now replays the cached
initialize, sends notifications/initialized and caches the new result.
A JSON-RPC error means the process was already initialized, so the
cache is kept; no answer clears it, so the next client handshake goes
to the backend.

This covers mode 2 of stacklok#6974. Mode 1, a restarted proxy facing a
backend that is still initialized, stays open.

Signed-off-by: somaz <genius5711@gmail.com>
somaz94 added a commit to somaz94/somaz94 that referenced this pull request Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant