Skip to content

Commit ee341df

Browse files
authored
Fix: Memory leak from unclosed sessions in Server (#359)
## Motivation and Context Sessions were added to the `sessions` list on connect but never removed when closed, causing a memory leak in long-running server processes. The closed sessions and their closures would accumulate indefinitely, preventing garbage collection. ## Breaking Changes None ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed
1 parent f27fe9a commit ee341df

File tree

1 file changed

+5
-0
lines changed
  • kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server

1 file changed

+5
-0
lines changed

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/Server.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ public open class Server(
169169
}
170170
}
171171

172+
// Register cleanup handler to remove session from list when it closes
173+
session.onClose {
174+
logger.debug { "Removing closed session from active sessions list" }
175+
sessions.update { list -> list - session }
176+
}
172177
logger.debug { "Server session connecting to transport" }
173178
session.connect(transport)
174179
logger.debug { "Server session successfully connected to transport" }

0 commit comments

Comments
 (0)