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
This commit is contained in:
Pavel Gorgulov 2025-11-03 16:36:53 +01:00 • committed by GitHub
commit ee341df11e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -169,6 +169,11 @@ public open class Server(
}
}
// Register cleanup handler to remove session from list when it closes
session.onClose {
logger.debug { "Removing closed session from active sessions list" }
sessions.update { list -> list - session }
}
logger.debug { "Server session connecting to transport" }
session.connect(transport)
logger.debug { "Server session successfully connected to transport" }