Close servlet streamable HTTP transports on async lifecycle events#1027
Open
lxq19991111 wants to merge 1 commit into
Open
Close servlet streamable HTTP transports on async lifecycle events#1027lxq19991111 wants to merge 1 commit into
lxq19991111 wants to merge 1 commit into
Conversation
This was referenced Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Register servlet async lifecycle cleanup for Streamable HTTP SSE transports created by
HttpServletStreamableServerTransportProvider.This makes replay GET streams and POST streaming responses close their current transport when the servlet async context completes, times out, or errors. It also routes SSE write failures through the transport
close()path instead of directly removing the logical MCP session.Fixes #1021
Motivation and Context
The servlet Streamable HTTP transport creates async SSE responses whose lifecycle is not wired back to SDK transport cleanup. When a client disconnects (sends TCP FIN), the server-side socket remains open because:
Sinks.Manyhas no subscribers but the stream is never terminatedAsyncContextis never completedCLOSE-WAITindefinitelyUnder moderate client churn (~50 disconnects), all Tomcat threads are exhausted within seconds, making the server completely unresponsive to new requests including health checks. This causes rolling deployments to fail in production (new pods get flooded by retrying clients immediately after startup).
Changes
close()path on handling failuresRationale
A TCP/SSE stream lifecycle is not the same as an MCP logical session lifecycle. A write failure or client disconnect proves that the current HTTP/SSE transport is no longer usable, but it does not necessarily mean the whole MCP session should be removed from the session registry.
This change closes the current transport and completes the associated servlet async context, while leaving session eviction to existing protocol-level paths such as DELETE, server shutdown, or future keep-alive/session-expiration work (see #1022 / #1028).
The async listener reuses the existing stream/transport close paths. These close paths are guarded and idempotent, so cleanup can be triggered consistently from servlet async completion, timeout, error, and write-failure paths.
Scope
This PR intentionally focuses on the MCP Java SDK core servlet transport (
HttpServletStreamableServerTransportProvider). Out of scope:HttpServletSseServerTransportProviderHow Has This Been Tested?
The added tests cover:
Full module test suite passes:
mvn -pl mcp-core testTypes of changes
Checklist