From ac7116b235f5feb732c5ed43ae3e599b33415e03 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Fri, 25 Sep 2026 16:31:20 +0300 Subject: [PATCH] Preserve vMCP resources until requests drain Keep workflow auditing and session resources available when HTTP draining times out, so callers can retry shutdown without losing terminal audit records. Preserve cleanup error chains during server construction and serving failures. Document drain ownership and retry limits, and cover incomplete shutdown, constructor rollback, and joined errors with regression tests. Signed-off-by: Juan Antonio Osorio --- docs/arch/vmcp-library.md | 25 ++ pkg/vmcp/server/audit_shutdown_test.go | 324 +++++++++++++++++++++++++ pkg/vmcp/server/server.go | 51 ++-- 3 files changed, 373 insertions(+), 27 deletions(-) create mode 100644 pkg/vmcp/server/audit_shutdown_test.go diff --git a/docs/arch/vmcp-library.md b/docs/arch/vmcp-library.md index 39fb76434c..d349dcd3f1 100644 --- a/docs/arch/vmcp-library.md +++ b/docs/arch/vmcp-library.md @@ -34,6 +34,31 @@ The `pkg/vmcp/` root package (`github.com/stacklok/toolhive/pkg/vmcp`) contains This is the same path used by `pkg/vmcp/cli/serve.go` in the `thv vmcp serve` command; the library has no CLI-specific coupling. +### Shutdown ownership and incomplete drains + +When serving the handler returned by `Server.Handler` in another HTTP server or +framework, embedders **must stop accepting requests and fully drain that hosting +server before calling `Server.Stop`**. `Stop` cannot drain an externally hosted +handler. If the hosting server's drain is incomplete, leave vMCP cleanup pending +and retry that drain with a fresh context before calling `Stop`. + +For the HTTP server owned by `Server.Start`, `Stop` allows up to 10 seconds (or the +caller's earlier context deadline) for HTTP draining. This is **not a bound on total +cleanup time**. If HTTP shutdown fails, `Stop` returns the error without +force-closing active connections or closing request-used resources, including the +core, workflow audit writer, and session storage. The listener is closed, but +cleanup remains pending. The caller may retry `Stop` serially with a fresh context +to finish draining and then release resources. This retry contract applies only to +incomplete HTTP drains: `Stop` does not support arbitrary repeated or concurrent +calls; the session manager's `Stop` is not idempotent. + +Direct core callers must prevent new calls and wait for in-flight calls to finish +before `core.VMCP.Close`; `Close` does not drain calls itself. + +The CLI retains its existing error-and-exit policy rather than retrying an +incomplete drain. Pending cleanup does not guarantee terminal workflow audit +records will be written if the process terminates before requests finish. + ## `pkg/vmcp/` Stability Table The table below maps every sub-package to its stability level per RFC THV-0059. Verify against the merged RFC if there is a discrepancy. diff --git a/pkg/vmcp/server/audit_shutdown_test.go b/pkg/vmcp/server/audit_shutdown_test.go new file mode 100644 index 0000000000..f440bd6f44 --- /dev/null +++ b/pkg/vmcp/server/audit_shutdown_test.go @@ -0,0 +1,324 @@ +// SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package server + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net" + "net/http" + "os" + "path/filepath" + "runtime" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + "github.com/stacklok/toolhive/pkg/audit" + transportsession "github.com/stacklok/toolhive/pkg/transport/session" + "github.com/stacklok/toolhive/pkg/vmcp" + "github.com/stacklok/toolhive/pkg/vmcp/aggregator" + aggmocks "github.com/stacklok/toolhive/pkg/vmcp/aggregator/mocks" + "github.com/stacklok/toolhive/pkg/vmcp/composer" + vmcpconfig "github.com/stacklok/toolhive/pkg/vmcp/config" + "github.com/stacklok/toolhive/pkg/vmcp/core" + vmcpmocks "github.com/stacklok/toolhive/pkg/vmcp/mocks" + routermocks "github.com/stacklok/toolhive/pkg/vmcp/router/mocks" + "github.com/stacklok/toolhive/pkg/vmcp/server/sessionmanager" + sessionmocks "github.com/stacklok/toolhive/pkg/vmcp/session/mocks" +) + +func TestStopIncompleteDrainPreservesWorkflowAuditUntilRetry(t *testing.T) { + t.Parallel() + + ctrl := gomock.NewController(t) + backend := vmcpmocks.NewMockBackendClient(ctrl) + agg := aggmocks.NewMockAggregator(ctrl) + target := &vmcp.BackendTarget{WorkloadID: "backend"} + agg.EXPECT().AggregateCapabilities(gomock.Any(), gomock.Any()).Return(&aggregator.AggregatedCapabilities{ + Tools: []vmcp.Tool{{Name: "echo", BackendID: "backend"}}, + RoutingTable: &vmcp.RoutingTable{Tools: map[string]*vmcp.BackendTarget{"echo": target}}, + }, nil).AnyTimes() + + entered, release := make(chan struct{}), make(chan struct{}) + unblock := sync.OnceFunc(func() { close(release) }) + backend.EXPECT().CallTool(gomock.Any(), target, "echo", gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( + func(context.Context, *vmcp.BackendTarget, string, map[string]any, map[string]any, map[string]string) (*vmcp.ToolCallResult, error) { + close(entered) + // Deliberately ignore request cancellation: Stop must not tear down + // resources while an uncooperative backend call is still running. + select { + case <-release: + return &vmcp.ToolCallResult{StructuredContent: map[string]any{"ok": true}}, nil + case <-time.After(30 * time.Second): + return nil, errors.New("timeout waiting to release backend") + } + }) + + auditPath := filepath.Join(t.TempDir(), "workflow-audit.log") + registry := vmcp.NewImmutableRegistry([]vmcp.Backend{{ID: "backend"}}) + v, err := core.New(&core.Config{ + Aggregator: agg, Router: routermocks.NewMockRouter(ctrl), BackendClient: backend, BackendRegistry: registry, + AuditConfig: &audit.Config{LogFile: auditPath, EventTypes: []string{ + audit.EventTypeWorkflowStarted, audit.EventTypeWorkflowCompleted, + }}, + WorkflowDefs: map[string]*composer.WorkflowDefinition{ + "workflow": {Name: "workflow", Steps: []composer.WorkflowStep{{ID: "step", Type: composer.StepTypeTool, Tool: "echo"}}}, + }, + }) + require.NoError(t, err) + t.Cleanup(func() { assert.NoError(t, v.Close()) }) + + cfg := testMinimalServeConfig() + cfg.BackendRegistry = registry + cfg.SessionManagerConfig = &sessionmanager.FactoryConfig{Base: sessionmocks.NewMockMultiSessionFactory(ctrl)} + // Only the core owns the file-backed workflow auditor; HTTP request auditing + // is independent of the resource whose shutdown ordering is under test. + srv, err := Serve(t.Context(), v, cfg) + require.NoError(t, err) + cleanupRan := false + srv.shutdownFuncs = append(srv.shutdownFuncs, func(context.Context) error { + cleanupRan = true + return nil + }) + requestCtx, cancelRequest := context.WithTimeout(context.Background(), 30*time.Second) + t.Cleanup(cancelRequest) + client := &http.Client{Transport: &http.Transport{}} + t.Cleanup(client.CloseIdleConnections) + var requestDone chan struct{} + t.Cleanup(func() { + unblock() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if !cleanupRan { + assert.NoError(t, srv.Stop(ctx)) + } + if srv.httpServer != nil { + assert.NoError(t, srv.httpServer.Close()) + } + if requestDone != nil { + select { + case <-requestDone: + case <-ctx.Done(): + t.Error("timeout waiting for HTTP client teardown") + } + } + }) + + handler, err := srv.Handler(t.Context()) + require.NoError(t, err) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + srv.listener = listener + ownedHTTP := &http.Server{Handler: handler, ReadHeaderTimeout: time.Second} + srv.httpServer = ownedHTTP + serveDone := make(chan error, 1) + go func() { serveDone <- ownedHTTP.Serve(listener) }() + + metadata := map[string]string{"state": "in-flight"} + created, err := srv.sessionDataStorage.Create(t.Context(), "pending", metadata) + require.NoError(t, err) + require.True(t, created) + + req, err := http.NewRequestWithContext(requestCtx, http.MethodPost, "http://"+listener.Addr().String()+"/mcp", + bytes.NewBufferString(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"workflow","arguments":{},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}`)) + require.NoError(t, err) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("MCP-Protocol-Version", "2026-07-28") + req.Header.Set("Mcp-Method", "tools/call") + req.Header.Set("Mcp-Name", "workflow") + var responseBody []byte + var responseStatus int + var requestErr error + requestDone = make(chan struct{}) + go func() { + defer close(requestDone) + resp, err := client.Do(req) + if err != nil { + requestErr = err + return + } + defer resp.Body.Close() + responseStatus = resp.StatusCode + responseBody, requestErr = io.ReadAll(resp.Body) + }() + select { + case <-entered: + case <-requestDone: + t.Fatalf("request finished before reaching backend: %v, %s", requestErr, responseBody) + case <-time.After(5 * time.Second): + t.Fatal("timeout waiting for workflow to reach backend") + } + + for attempt := range 2 { + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Millisecond) + err := srv.Stop(ctx) + cancel() + require.ErrorIs(t, err, context.DeadlineExceeded, "drain attempt %d", attempt+1) + require.False(t, cleanupRan, "incomplete drain must not run shutdown functions") + require.Same(t, ownedHTTP, srv.httpServer, "retry must retain the server with active connections") + require.Nil(t, srv.listener, "Shutdown has closed the listener") + stored, err := srv.sessionDataStorage.Load(t.Context(), "pending") + require.NoError(t, err, "in-flight session metadata must survive") + require.Equal(t, metadata, stored) + select { + case <-requestDone: + t.Fatalf("Stop interrupted the active request: %v, %s", requestErr, responseBody) + default: + } + } + select { + case err := <-serveDone: + require.ErrorIs(t, err, http.ErrServerClosed) + case <-time.After(5 * time.Second): + t.Fatal("listener did not stop serving") + } + + unblock() + select { + case <-requestDone: + case <-time.After(5 * time.Second): + t.Fatal("workflow did not complete after backend release") + } + require.NoError(t, requestErr) + require.Equal(t, http.StatusOK, responseStatus, "%s", responseBody) + var envelope struct { + Error json.RawMessage `json:"error"` + Result struct { + IsError bool `json:"isError"` + StructuredContent map[string]any `json:"structuredContent"` + } `json:"result"` + } + require.NoError(t, json.Unmarshal(responseBody, &envelope)) + require.Empty(t, envelope.Error) + require.False(t, envelope.Result.IsError) + require.Equal(t, true, envelope.Result.StructuredContent["ok"]) + require.False(t, cleanupRan) + + data, err := os.ReadFile(auditPath) + require.NoError(t, err) + lines := bytes.Split(bytes.TrimSpace(data), []byte("\n")) + require.Len(t, lines, 2, "started and terminal records must exist before cleanup") + var started, completed struct { + Type string `json:"type"` + Outcome string `json:"outcome"` + Target map[string]string `json:"target"` + } + require.NoError(t, json.Unmarshal(lines[0], &started)) + require.NoError(t, json.Unmarshal(lines[1], &completed)) + assert.Equal(t, audit.EventTypeWorkflowStarted, started.Type) + assert.Equal(t, audit.EventTypeWorkflowCompleted, completed.Type) + assert.Equal(t, audit.OutcomeSuccess, completed.Outcome) + assert.Equal(t, "workflow", completed.Target[audit.TargetKeyWorkflowName]) + require.NotEmpty(t, started.Target[audit.TargetKeyWorkflowID]) + assert.Equal(t, started.Target[audit.TargetKeyWorkflowID], completed.Target[audit.TargetKeyWorkflowID]) + + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + t.Cleanup(cancel) + require.NoError(t, srv.Stop(ctx), "fresh-context retry must finish cleanup") + require.True(t, cleanupRan) + _, err = srv.sessionDataStorage.Load(t.Context(), "pending") + require.ErrorIs(t, err, transportsession.ErrSessionNotFound, "successful Stop must close storage") + if runtime.GOOS == "linux" { + requireNoAuditFD(t, auditPath) + } +} + +func TestNewServeFailureReleasesWorkflowAuditFile(t *testing.T) { + t.Parallel() + if runtime.GOOS != "linux" { + t.Skip("file descriptor release is checked through /proc/self/fd on Linux") + } + ctrl := gomock.NewController(t) + path := filepath.Join(t.TempDir(), "workflow-audit.log") + srv, err := New(t.Context(), &Config{ + Aggregator: aggmocks.NewMockAggregator(ctrl), + SessionFactory: sessionmocks.NewMockMultiSessionFactory(ctrl), + AuditConfig: &audit.Config{LogFile: path}, + SessionStorage: &vmcpconfig.SessionStorageConfig{Provider: "unsupported"}, + }, routermocks.NewMockRouter(ctrl), vmcpmocks.NewMockBackendClient(ctrl), vmcp.NewImmutableRegistry(nil), nil) + if srv != nil { + t.Cleanup(func() { assert.NoError(t, srv.Stop(context.Background())) }) + } + require.Nil(t, srv) + require.ErrorContains(t, err, "failed to create session data storage: unsupported session storage provider") + _, statErr := os.Stat(path) + require.NoError(t, statErr, "core.New must have opened the audit file before Serve failed") + requireNoAuditFD(t, path) +} + +func TestStartJoinsHTTPAndStopErrors(t *testing.T) { + t.Parallel() + ctrl := gomock.NewController(t) + srv, err := New(t.Context(), &Config{ + Host: "127.0.0.1", Port: 0, + Aggregator: aggmocks.NewMockAggregator(ctrl), SessionFactory: sessionmocks.NewMockMultiSessionFactory(ctrl), + }, routermocks.NewMockRouter(ctrl), vmcpmocks.NewMockBackendClient(ctrl), vmcp.NewImmutableRegistry(nil), nil) + require.NoError(t, err) + stopErr := errors.New("shutdown sentinel") + cleanupRan := false + srv.shutdownFuncs = append(srv.shutdownFuncs, func(context.Context) error { + cleanupRan = true + return stopErr + }) + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan struct{}) + var startErr error + go func() { + defer close(done) + startErr = srv.Start(ctx) + }() + t.Cleanup(func() { + cancel() + select { + case <-done: + if !cleanupRan { + assert.ErrorIs(t, srv.Stop(context.Background()), stopErr) + } + case <-time.After(5 * time.Second): + t.Error("timeout waiting for Start teardown") + } + }) + select { + case <-srv.Ready(): + case <-done: + t.Fatalf("Start failed before readiness: %v", startErr) + case <-time.After(5 * time.Second): + t.Fatal("timeout waiting for Start readiness") + } + srv.listenerMu.RLock() + listener := srv.listener + srv.listenerMu.RUnlock() + require.NoError(t, listener.Close()) + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Start did not return after listener failure") + } + assert.ErrorIs(t, startErr, net.ErrClosed) + assert.ErrorIs(t, startErr, stopErr, "Start must preserve Stop's error chain") +} + +func requireNoAuditFD(t *testing.T, path string) { + t.Helper() + resolvedPath, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + entries, err := os.ReadDir("/proc/self/fd") + require.NoError(t, err) + for _, entry := range entries { + target, err := os.Readlink(filepath.Join("/proc/self/fd", entry.Name())) + if errors.Is(err, os.ErrNotExist) { + continue // Descriptors in parallel tests can close during enumeration. + } + require.NoError(t, err) + require.NotEqual(t, resolvedPath, target, "workflow audit file descriptor must be released") + } +} diff --git a/pkg/vmcp/server/server.go b/pkg/vmcp/server/server.go index a2b8de4a6f..4832ff0188 100644 --- a/pkg/vmcp/server/server.go +++ b/pkg/vmcp/server/server.go @@ -71,7 +71,7 @@ const ( // defaultMaxHeaderBytes is the maximum size of request headers in bytes (1 MB). defaultMaxHeaderBytes = 1 << 20 - // defaultShutdownTimeout is the maximum time to wait for graceful shutdown. + // defaultShutdownTimeout bounds HTTP draining, not the subsequent resource cleanup. defaultShutdownTimeout = 10 * time.Second // defaultHeartbeatInterval sends SSE heartbeat pings on GET connections. @@ -529,22 +529,11 @@ func New( // ListTools now advertises execute_tool_script alongside the backend tools, so the // Serve-layer optimizer (if enabled) indexes it like any other tool, and the script's // inner calls route back through core.CallTool for admission. The decorator delegates - // Close to the inner core, so the closeCoreOnErr guard below still releases it. + // Close to the inner core, so New can release it if Serve fails and srv.Stop owns it on success. if cfg.CodeModeConfig != nil { coreVMCP = codemode.NewDecorator(coreVMCP, cfg.CodeModeConfig) } - // core.New started the workflow state store's cleanup goroutine and the backend health - // monitor (both owned by the core now). If Serve fails after this point, close the core so - // neither leaks (mirrors Serve's closeStorageOnErr guard); on success the core's lifecycle - // is owned by srv.Stop, so the guard is disarmed before returning. - closeCoreOnErr := true - defer func() { - if closeCoreOnErr { - _ = coreVMCP.Close() - } - }() - // On the New/Serve path the core is the single aggregator and the source of the // advertised set; the session factory only opens per-session backend connections and // binds identity. AdvertiseFromCore makes the session manager source tools from the @@ -574,9 +563,10 @@ func New( BackendHealth: coreVMCP.BackendHealth(), } + // On success srv.Stop owns the core; on failure New must release it. srv, err := Serve(ctx, coreVMCP, deriveServerConfig(resolved, backendRegistry, sessMgrCfg)) if err != nil { - return nil, err + return nil, errors.Join(err, coreVMCP.Close()) } // Enable the pre-dispatch authorization gate when Cedar policies are configured. @@ -603,12 +593,14 @@ func New( ) } - closeCoreOnErr = false // Serve succeeded; srv.Stop now owns the core's lifecycle. return srv, nil } // Handler builds and returns the MCP HTTP handler without starting a listener. // This enables embedding the vmcp server inside another HTTP server or framework. +// Embedders must stop accepting requests and fully drain their hosting HTTP server +// before calling Stop; Stop cannot drain an externally hosted handler. +// If the hosting server's drain is incomplete, retry it with a fresh context before Stop. // // The returned handler includes all routes (health, metrics, well-known, MCP) // and the full HTTP middleware chain (recovery, body limit, header validation, @@ -930,15 +922,18 @@ func (s *Server) Start(ctx context.Context) error { case err := <-errCh: // HTTP server error - log and tear down cleanly slog.Error("hTTP server error", "error", err) - if stopErr := s.Stop(context.Background()); stopErr != nil { - // Combine errors if Stop() also fails - return fmt.Errorf("server error: %w; stop error: %v", err, stopErr) - } - return err + return errors.Join(err, s.Stop(context.Background())) } } // Stop gracefully stops the Virtual MCP Server. +// It allows up to 10 seconds (or ctx's earlier deadline) for the owned HTTP server +// to drain, then releases resources. This bounds HTTP draining, not total cleanup. +// If draining fails, Stop returns without force-closing active connections or +// releasing request-used resources. Cleanup remains pending; callers may retry +// Stop serially with a fresh context after an incomplete drain. Stop does not +// support arbitrary repeated or concurrent calls. +// Embedders using Handler must fully drain their hosting HTTP server before Stop. func (s *Server) Stop(ctx context.Context) error { slog.Info("stopping Virtual MCP Server") @@ -950,16 +945,18 @@ func (s *Server) Stop(ctx context.Context) error { shutdownCtx, cancel := context.WithTimeout(ctx, defaultShutdownTimeout) defer cancel() - if err := s.httpServer.Shutdown(shutdownCtx); err != nil { - errs = append(errs, fmt.Errorf("failed to shutdown HTTP server: %w", err)) + err := s.httpServer.Shutdown(shutdownCtx) + + // Shutdown closes the listener even if draining active requests fails. + s.listenerMu.Lock() + s.listener = nil + s.listenerMu.Unlock() + + if err != nil { + return fmt.Errorf("failed to shutdown HTTP server: %w", err) } } - // Clear listener reference (already closed by httpServer.Shutdown) - s.listenerMu.Lock() - s.listener = nil - s.listenerMu.Unlock() - if err := s.stopDiagnostics(ctx); err != nil { errs = append(errs, err) }