From 667bdba309aad702b3f1bb36a653ff5750163343 Mon Sep 17 00:00:00 2001 From: oplatonov Date: Sun, 20 Sep 2026 01:13:00 +0300 Subject: [PATCH] mcp: log a keepalive session close at Warn, not Error When the keepalive failure threshold is reached, startKeepalive logs "keepalive ping failed; closing session" at Error. In practice this is almost always a client that went away, the normal end of a session, so on a long-running server these records drown out real errors. Log it at Warn, the same level as the below-threshold case and as a failed notification delivery in notifySessions. Fixes #1281 --- mcp/mcp_test.go | 6 +++--- mcp/shared.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mcp/mcp_test.go b/mcp/mcp_test.go index 7b10b30f..c12c0a1e 100644 --- a/mcp/mcp_test.go +++ b/mcp/mcp_test.go @@ -1971,7 +1971,7 @@ func TestKeepAliveFailure_Logged(t *testing.T) { var buf bytes.Buffer clientOpts := &ClientOptions{ KeepAlive: 50 * time.Millisecond, - Logger: slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelError})), + Logger: slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn})), } c := NewClient(testImpl, clientOpts) // Pin to 2025-11-25: KeepAlive uses the ping RPC, which is removed @@ -1992,8 +1992,8 @@ func TestKeepAliveFailure_Logged(t *testing.T) { synctest.Wait() got := buf.String() // slog serializes Write calls internally - if !strings.Contains(got, "keepalive ping failed") { - t.Errorf("expected keepalive failure to be logged, got log output:\n%s", got) + if !strings.Contains(got, `level=WARN msg="keepalive ping failed; closing session"`) { + t.Errorf("expected keepalive failure to be logged at Warn, got log output:\n%s", got) } }) } diff --git a/mcp/shared.go b/mcp/shared.go index cacfa75d..69501a03 100644 --- a/mcp/shared.go +++ b/mcp/shared.go @@ -961,7 +961,7 @@ func startKeepalive(session keepaliveSession, interval time.Duration, failureThr } // Threshold reached; log before closing the session so the // failure is observable to operators. See #218. - logger.Error("keepalive ping failed; closing session", + logger.Warn("keepalive ping failed; closing session", "error", err, "consecutiveFailures", consecutiveFailures, "failureThreshold", failureThreshold)