mcp: don't persist failed streamable sessions (#605)
If a stateful streamable session fails to initialize, it is unusable. Avoid allocating resources by closing the session immediately. Also: - Fix a bug where InitializeParams is not guarded with its mutex. - Fix a resource leak where sessions are persisted with session id "", even though they are unaddressable. - Add a relevant benchmark, and update tests. Fixes #578
This commit is contained in:
parent
ae6bda692b
commit
d256a9c109
4 changed files with 113 additions and 7 deletions
|
|
@ -1145,7 +1145,13 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
|
||||||
return handleReceive(ctx, ss, req)
|
return handleReceive(ctx, ss, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *ServerSession) InitializeParams() *InitializeParams { return ss.state.InitializeParams }
|
// InitializeParams returns the InitializeParams provided during the client's
|
||||||
|
// initial connection.
|
||||||
|
func (ss *ServerSession) InitializeParams() *InitializeParams {
|
||||||
|
ss.mu.Lock()
|
||||||
|
defer ss.mu.Unlock()
|
||||||
|
return ss.state.InitializeParams
|
||||||
|
}
|
||||||
|
|
||||||
func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParams) (*InitializeResult, error) {
|
func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParams) (*InitializeResult, error) {
|
||||||
if params == nil {
|
if params == nil {
|
||||||
|
|
|
||||||
|
|
@ -324,10 +324,13 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
||||||
logger: h.opts.Logger,
|
logger: h.opts.Logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sessions without a session ID are also stateless: there's no way to
|
||||||
|
// address them.
|
||||||
|
stateless := h.opts.Stateless || sessionID == ""
|
||||||
// To support stateless mode, we initialize the session with a default
|
// To support stateless mode, we initialize the session with a default
|
||||||
// state, so that it doesn't reject subsequent requests.
|
// state, so that it doesn't reject subsequent requests.
|
||||||
var connectOpts *ServerSessionOptions
|
var connectOpts *ServerSessionOptions
|
||||||
if h.opts.Stateless {
|
if stateless {
|
||||||
// Peek at the body to see if it is initialize or initialized.
|
// Peek at the body to see if it is initialize or initialized.
|
||||||
// We want those to be handled as usual.
|
// We want those to be handled as usual.
|
||||||
var hasInitialize, hasInitialized bool
|
var hasInitialize, hasInitialized bool
|
||||||
|
|
@ -405,7 +408,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
||||||
transport: transport,
|
transport: transport,
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.opts.Stateless {
|
if stateless {
|
||||||
// Stateless mode: close the session when the request exits.
|
// Stateless mode: close the session when the request exits.
|
||||||
defer session.Close() // close the fake session after handling the request
|
defer session.Close() // close the fake session after handling the request
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -424,6 +427,13 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
h.sessions[transport.SessionID] = sessInfo
|
h.sessions[transport.SessionID] = sessInfo
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
|
defer func() {
|
||||||
|
// If initialization failed, clean up the session (#578).
|
||||||
|
if session.InitializeParams() == nil {
|
||||||
|
// Initialization failed.
|
||||||
|
session.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,15 @@ package mcp_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/jsonschema-go/jsonschema"
|
"github.com/google/jsonschema-go/jsonschema"
|
||||||
|
|
@ -65,3 +71,62 @@ func BenchmarkStreamableServing(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var streamableHeap = flag.String("streamable_heap", "", "if set, write streamable heap profiles with this prefix")
|
||||||
|
|
||||||
|
func BenchmarkStreamableServing_BadSessions(b *testing.B) {
|
||||||
|
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
|
||||||
|
|
||||||
|
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
|
||||||
|
return server
|
||||||
|
}, &mcp.StreamableHTTPOptions{JSONResponse: true})
|
||||||
|
|
||||||
|
httpServer := httptest.NewServer(handler)
|
||||||
|
defer httpServer.Close()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if *streamableHeap != "" {
|
||||||
|
writeHeap := func(file string) {
|
||||||
|
// GC a couple times to ensure accurate heap.
|
||||||
|
runtime.GC()
|
||||||
|
runtime.GC()
|
||||||
|
f, err := os.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not create memory profile: ", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
b.Errorf("writing heap file %q: %v", file, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err := pprof.Lookup("heap").WriteTo(f, 0); err != nil {
|
||||||
|
b.Errorf("could not write heap profile: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeHeap(*streamableHeap + ".before")
|
||||||
|
defer writeHeap(*streamableHeap + ".after")
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for range b.N {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, httpServer.URL, strings.NewReader("{}"))
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
req.Header.Add("Accept", "text/event-stream")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
|
||||||
|
b.Fatalf("POST got status %d, want %d", got, want)
|
||||||
|
}
|
||||||
|
if got := resp.Header.Get("Mcp-Session-Id"); got != "" {
|
||||||
|
b.Fatalf("POST got unexpected session ID")
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -690,10 +690,11 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
replay bool // if set, use a MemoryEventStore to enable stream replay
|
replay bool // if set, use a MemoryEventStore to enable stream replay
|
||||||
tool func(*testing.T, context.Context, *ServerSession)
|
tool func(*testing.T, context.Context, *ServerSession)
|
||||||
requests []streamableRequest // http requests
|
requests []streamableRequest // http requests
|
||||||
|
wantSessions int // number of sessions expected after the test
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic",
|
name: "basic",
|
||||||
|
|
@ -707,6 +708,19 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
wantMessages: []jsonrpc.Message{resp(2, &CallToolResult{Content: []Content{}}, nil)},
|
wantMessages: []jsonrpc.Message{resp(2, &CallToolResult{Content: []Content{}}, nil)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uninitialized",
|
||||||
|
requests: []streamableRequest{
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
messages: []jsonrpc.Message{req(2, "tools/call", &CallToolParams{Name: "tool"})},
|
||||||
|
wantStatusCode: http.StatusOK,
|
||||||
|
wantBodyContaining: "invalid during session initialization",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantSessions: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "accept headers",
|
name: "accept headers",
|
||||||
|
|
@ -741,6 +755,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
wantMessages: []jsonrpc.Message{resp(4, &CallToolResult{Content: []Content{}}, nil)},
|
wantMessages: []jsonrpc.Message{resp(4, &CallToolResult{Content: []Content{}}, nil)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "protocol version headers",
|
name: "protocol version headers",
|
||||||
|
|
@ -756,6 +771,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
wantSessionID: false, // could be true, but shouldn't matter
|
wantSessionID: false, // could be true, but shouldn't matter
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "batch rejected on 2025-06-18",
|
name: "batch rejected on 2025-06-18",
|
||||||
|
|
@ -775,6 +791,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
wantBodyContaining: "batch",
|
wantBodyContaining: "batch",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "batch accepted on 2025-03-26",
|
name: "batch accepted on 2025-03-26",
|
||||||
|
|
@ -797,6 +814,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "tool notification",
|
name: "tool notification",
|
||||||
|
|
@ -821,6 +839,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "tool upcall",
|
name: "tool upcall",
|
||||||
|
|
@ -853,6 +872,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "background",
|
name: "background",
|
||||||
|
|
@ -915,6 +935,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
headers: map[string][]string{"Accept": nil},
|
headers: map[string][]string{"Accept": nil},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 0, // session deleted
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "errors",
|
name: "errors",
|
||||||
|
|
@ -946,6 +967,7 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
})},
|
})},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantSessions: 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -972,6 +994,9 @@ func TestStreamableServerTransport(t *testing.T) {
|
||||||
defer handler.closeAll()
|
defer handler.closeAll()
|
||||||
|
|
||||||
testStreamableHandler(t, handler, test.requests)
|
testStreamableHandler(t, handler, test.requests)
|
||||||
|
if got := len(slices.Collect(server.Sessions())); got != test.wantSessions {
|
||||||
|
t.Errorf("after test, got %d sessions, want %d", got, test.wantSessions)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue