fix: release transport-owned HttpClient references on close#904
Open
TsengX wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: release transport-owned HttpClient references on close#904TsengX wants to merge 1 commit intomodelcontextprotocol:mainfrom
TsengX wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
9 tasks
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
Fixes the Java 17
HttpClient-* SelectorManagerthread retention reported in#547and#620by releasing transport-ownedHttpClientreferences on close instead of forcing internal client shutdown.This change:
Unsafe, and JDK-internal shutdown hookscloseGracefully()non-blockingThis is an alternative to the approaches explored in
#610and#868.Motivation and Context
The root problem was that
HttpClientSseClientTransportandHttpClientStreamableHttpTransportretained a strong reference to their internally createdHttpClienteven after transport close.If user code retained closed transport objects, those internal
HttpClientinstances also remained reachable, which in turn keptHttpClient-* SelectorManagerthreads alive.Instead of trying to forcibly shut down JDK
HttpClientinternals, this PR treats the issue as an ownership problem:HttpClientAdditionally,
DefaultMcpTransportSession.closeGracefully()now always disposes tracked connections even ifonClosefails, which makes close-path cleanup reliable.How Has This Been Tested?
Locally verified with targeted
mcp-coreregression tests:DefaultMcpTransportSessionTestsHttpClientSseClientTransportLeakTestsHttpClientStreamableHttpTransportLeakTestsThese tests:
HttpClient-* SelectorManagerthreads return to baseline after GC stabilizationAlso verified with existing Docker-backed integration tests:
HttpClientSseClientTransportTestsHttpClientStreamableHttpTransportTestBreaking Changes
None.
Types of changes
Checklist
Additional context
Implementation notes:
OwnedHttpClientabstraction to track transport-owned clients.HttpClientSseClientTransportandHttpClientStreamableHttpTransportnow release their owned client reference on close.DefaultMcpTransportSession.closeGracefully()now disposes tracked connections even whenonCloseerrors.HttpClientinternals.