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()pathinstead of directly removing the logical MCP session.
Motivation
The core servlet Streamable HTTP transport can create async SSE responses whose lifecycle is not consistently wired
back to SDK transport cleanup. If a client disconnects, the server-side socket can remain open until a later write
failure, container timeout, or server shutdown.
In practice, this can leave server-side sockets in
CLOSE-WAITbecause the remote peer has sent FIN but the serverapplication has not completed the corresponding servlet async response. Since these streams use
AsyncContextwith notimeout, the transport should explicitly clean up the current stream when servlet async lifecycle events indicate
completion, timeout, or error.
This PR intentionally focuses on the MCP Java SDK core servlet transport. It does not add session TTL, keep-alive
failed-ping eviction, or any Spring-specific WebMVC follow-up.
Changes
close()path on handling failures.failure behavior.
Rationale
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.
For that reason, 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.
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.
Testing
Ran the focused mcp-core test class: