You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1bdfcd6
Browse filesBrowse the repository at this point in the historyBrowse files
fix: release MCP session state so the server stops leaking heap (#85)
* fix: release MCP session state so the server stops leaking heap
The streamable HTTP server registers a session on `initialize` and, on
mcp-go v0.43.2, never releases it: `handleDelete` clears the per-session
stores but never calls `UnregisterSession`, so every session a client ever
opened is retained. A POST-only client leaks roughly 8 KB per initialize,
which in production shows up as linear heap growth until the container is
OOMKilled.
Bump mcp-go to v1.1.0, which unregisters the session on DELETE, and enable
its idle sweeper so sessions abandoned without a DELETE are reclaimed too.
The sweeper is opt-in even on v1.1.0, so the bump alone is not enough. The
new `--session-idle-ttl` flag defaults to 10m and 0 disables it. Shutdown
now stops the sweeper and closes sessions that are still registered.
Measured over repeated bursts of 500 initialize requests, 15s apart:
mcp-go v0.43.2 3.44 -> 7.74 -> 12.96 -> 17.96 -> 21.65 MB live heap
this change 3.93 -> 9.55 -> 9.63 -> 9.55 -> 9.55 MB live heap
Closes#84
Signed-off-by: younsl <cysl@kakao.com>
* test: run the cmd package tests in make test
`make test` covered ./pkg/... and ./internal/... only, so the tests in cmd/
never ran in CI. That includes the session lifecycle regression tests this
branch adds, which is the code path that leaked.
Signed-off-by: younsl <cysl@kakao.com>
---------
Signed-off-by: younsl <cysl@kakao.com>
Co-authored-by: Dmytro Rashko <dimetron@me.com>
Copy file name to clipboardExpand all lines: cmd/main.go
+35-10Lines changed: 35 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,8 @@ var (
46
46
showVersionbool
47
47
readOnlybool
48
48
49
+
sessionIdleTTL time.Duration
50
+
49
51
// These variables should be set during build time using -ldflags
50
52
Name="kagent-tools-server"
51
53
Version=version.Version
@@ -66,6 +68,7 @@ func init() {
66
68
rootCmd.Flags().StringSliceVar(&tools, "tools", []string{}, "List of tools to register. If empty, all tools are registered.")
67
69
rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "Show version information and exit")
68
70
rootCmd.Flags().BoolVar(&readOnly, "read-only", false, "Run in read-only mode (disable tools that perform write operations)")
71
+
rootCmd.Flags().DurationVar(&sessionIdleTTL, "session-idle-ttl", 10*time.Minute, "Reclaim streamable HTTP session state after this idle duration (0 disables the sweeper)")
69
72
kubeconfig=rootCmd.Flags().String("kubeconfig", "", "kubeconfig file path (optional, defaults to in-cluster config)")
0 commit comments