Skip to content

Agent Mesh

address review: only start the drain timeout once the backend leg has ended

Fernando Marino57b5c53bda5e
diff --git a/internal/node/gate_test.go b/internal/node/gate_test.go
index e4a30ce5..b67d06e5 100644
--- a/internal/node/gate_test.go
+++ b/internal/node/gate_test.go
@@ -450,3 +450,81 @@ func TestHandleStreamPassThrough_BackendEOFDoesNotDropInFlightResponse(t *testin
}()
}
}
+
+// TestHandleStreamPassThrough_SlowBackendDoesNotHitDrainTimeout is a
+// regression test for review feedback from aojea on google/sam#379: the
+// drain wait was timed from the start of the whole exchange, not from when
+// the backend leg actually finished, so any session - healthy or not -
+// that happened to run longer than passThroughDrainTimeout got killed
+// mid-flight. Shrinks passThroughDrainTimeout for the test so proving this
+// doesn't need a multi-second sleep; startBareNode's plain TCP transport is
+// enough here, since this is about goroutine timing, not the QUIC-specific
+// Close semantics TestHandleStreamPassThrough_BackendEOFDoesNotDropInFlightResponse
+// covers.
+func TestHandleStreamPassThrough_SlowBackendDoesNotHitDrainTimeout(t *testing.T) {
+ oldTimeout := passThroughDrainTimeout
+ passThroughDrainTimeout = 50 * time.Millisecond
+ t.Cleanup(func() { passThroughDrainTimeout = oldTimeout })
+
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+
+ // Several times passThroughDrainTimeout: under the bug, the stream was
+ // torn down well before the backend ever answered.
+ const backendDelay = 300 * time.Millisecond
+ upstream := httptest.NewServer(mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
+ srv := mcp.NewServer(&mcp.Implementation{Name: "slow", Version: "0.0.1"}, nil)
+ srv.AddTool(&mcp.Tool{Name: "slow_echo", Description: "slow", InputSchema: map[string]any{"type": "object"}},
+ func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
+ time.Sleep(backendDelay)
+ return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: "ok"}}}, nil
+ })
+ return srv
+ }, nil))
+ defer upstream.Close()
+
+ nodeA, cleanupA := startBareNode(t, ctx)
+ defer cleanupA()
+ nodeB, cleanupB := startBareNode(t, ctx)
+ defer cleanupB()
+
+ svc := &MCPService{baseService: baseService{
+ info: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "slow-svc"},
+ backend: &api.RegisterServiceRequest_TargetUrl{TargetUrl: upstream.URL},
+ }}
+ if err := svc.Init(ctx); err != nil {
+ t.Fatalf("MCPService.Init: %v", err)
+ }
+ nodeA.services.insertService(svc)
+ t.Cleanup(func() { _ = svc.Teardown() })
+
+ nodeA.Host.SetStreamHandler(testMCPProtocol, func(s network.Stream) {
+ nodeA.HandleMCPStream(s, RequestContext{Target: "slow-svc"})
+ })
+
+ if err := nodeB.Host.Connect(ctx, peer.AddrInfo{ID: nodeA.Host.ID(), Addrs: nodeA.Host.Addrs()}); err != nil {
+ t.Fatalf("connect: %v", err)
+ }
+
+ s, err := nodeB.Host.NewStream(ctx, nodeA.Host.ID(), testMCPProtocol)
+ if err != nil {
+ t.Fatalf("NewStream: %v", err)
+ }
+ defer func() { _ = s.Close() }()
+
+ client := mcp.NewClient(&mcp.Implementation{Name: "tc", Version: "0.0.1"}, nil)
+ session, err := client.Connect(ctx, NewStreamTransport(s), nil)
+ if err != nil {
+ t.Fatalf("client.Connect: %v", err)
+ }
+ defer func() { _ = session.Close() }()
+
+ res, err := session.CallTool(ctx, &mcp.CallToolParams{Name: "slow_echo", Arguments: map[string]any{}})
+ if err != nil {
+ t.Fatalf("CallTool: %v (a healthy exchange slower than passThroughDrainTimeout must not be killed for that alone)", err)
+ }
+ tc, ok := res.Content[0].(*mcp.TextContent)
+ if !ok || tc.Text != "ok" {
+ t.Fatalf("got %v, want text %q", res.Content, "ok")
+ }
+}
diff --git a/internal/node/mcp_service.go b/internal/node/mcp_service.go
index 1575ff98..9cf376e4 100644
--- a/internal/node/mcp_service.go
+++ b/internal/node/mcp_service.go
@@ -145,7 +145,12 @@ var preflightMethodsUnsupportedByPassThrough = map[string]bool{
// client to finish reading the last response and hang up on its own. See the
// comment on the drain logic in HandleStreamPassThrough for why this wait
// exists at all.
-const passThroughDrainTimeout = 5 * time.Second
+//
+// A var, not a const, so a test can shrink it - the countdown only starts
+// once the backend leg is done, so shrinking it does not affect an
+// in-progress exchange, only how long a stalled client is tolerated for
+// after that.
+var passThroughDrainTimeout = 5 * time.Second
// HandleStreamPassThrough connects to the backend and proxies JSON-RPC messages.
func (m *MCPService) HandleStreamPassThrough(s network.Stream) {
@@ -201,8 +206,10 @@ func (m *MCPService) HandleStreamPassThrough(s network.Stream) {
// the read side alone. The client leg - the client itself finishing the
// read and hanging up, or a genuine transport error - is what triggers
// the final s.Close() at the top of this function. passThroughDrainTimeout
- // bounds that wait so a client that never hangs up can't leak the stream
- // forever.
+ // bounds that wait, but only once the backend leg is actually done
+ // (backendDone below) - it is not a cap on the whole exchange, or a
+ // slow-but-healthy session would be killed mid-flight for no better
+ // reason than having taken a while.
//
// clientErrc is buffered for 2, not 1: a client write failure below is
// also a client-leg error (the stream to the client is dead, so there is
@@ -210,8 +217,10 @@ func (m *MCPService) HandleStreamPassThrough(s network.Stream) {
// unlucky interleaving where the main select has already consumed one
// value could otherwise leave the second sender blocked forever.
clientErrc := make(chan error, 2)
+ backendDone := make(chan struct{})
go func() {
+ defer close(backendDone)
for {
msg, err := backendConn.Read(ctx)
if err != nil {
@@ -254,6 +263,15 @@ func (m *MCPService) HandleStreamPassThrough(s network.Stream) {
}
}()
+ // No timeout here: the exchange runs for as long as both legs are
+ // making progress. Only once the backend leg ends (backendDone) does a
+ // bounded wait for the client to also finish begin, below.
+ select {
+ case <-clientErrc:
+ return
+ case <-backendDone:
+ }
+
select {
case <-clientErrc:
case <-time.After(passThroughDrainTimeout):