modelcontextprotocol / modelcontextprotocol/java-sdk
McpClientSession/McpServerSession: pending response entries leak when a request times out or is cancelled
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Java
- Estrellas
- 3.7k
- Forks
- 1.1k
- Merge medio
- 1 d 15 h
- PR fusionados (30 d)
- 9
Descripción
Bug description
McpClientSession.sendRequest and McpServerSession.sendRequest put an entry into pendingResponses when a request is sent, but only remove it when a response arrives or the send itself fails. If the downstream .timeout(...) fires — or the caller cancels the Mono — no cleanup hook runs, so the entry stays in pendingResponses forever (request IDs are unique per request, so it is never overwritten).
The streamable variant already cleans up after its timeout (McpStreamableServerSession.McpStreamableServerSessionStream.sendRequest calls this.pendingResponses.remove(requestId) in a doOnError after .timeout(requestTimeout)), which suggests the legacy paths missing this is an oversight.
Amplifier
KeepAliveScheduler pings every session periodically via sendRequest(PING, ...). A dead or hung session therefore accumulates one leaked entry per ping interval on both the client and the server side; long-lived sessions grow unboundedly until close().
Suggested fix
Mirror the streamable pattern in both sendRequest methods:
.timeout(this.requestTimeout)
.doOnError(e -> this.pendingResponses.remove(requestId))
.doOnCancel(() -> this.pendingResponses.remove(requestId))
.handle(...)
Removal is idempotent, and the late-response path already tolerates a missing key.
I have the fix plus a regression test (testRequestTimeoutRemovesPendingResponse in McpClientSessionTests, asserting the map is empty after a timeout) ready and will open a PR referencing this issue.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con McpClientSession.sendRequest y McpServerSession.sendRequest; después, compáralos con McpStreamableServerSession.McpStreamableServerSessionStream.sendRequest. Ejecuta la prueba de regresión McpClientSessionTests mencionada y añade una cobertura equivalente para las rutas de timeout y cancelación; se considera terminado cuando pendingResponses está vacío después de cualquiera de las dos condiciones.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- java
- Área
- api, backend, testing
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Activo
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100