Fix HttpClient resource leak in HTTP transports #610
+230
−11
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.
Fixes thread accumulation issue where HttpClient-xxxx-SelectorManager threads would continuously grow, leading to memory exhaustion. This addresses the underlying JDK issue documented in JDK-8308364.
Related: https://bugs.openjdk.org/browse/JDK-8308364
Motivation and Context
The
HttpClientStreamableHttpTransport
andHttpClientSseClientTransport
classes create new HttpClient instances viaHttpClient.Builder.build()
without properly managing their lifecycle. This leads to accumulation ofHttpClient-xxxx-SelectorManager
threads that are never cleaned up, eventually causing memory exhaustion.Root cause analysis traced this to the OpenJDK 17 HttpClientImpl implementation (JDK-8308364), where each HttpClient spawns dedicated SelectorManager threads for network I/O but lacks public APIs for proper resource cleanup.
This change introduces two solutions:
References
How Has This Been Tested?
HttpClientStreamableHttpSyncClientTests
andHttpSseMcpSyncClientTests
Breaking Changes
No breaking changes. This is an additive change that maintains full API compatibility. Existing code will continue to work as before, but will now benefit from proper resource management and the option to use external HttpClient instances.
Types of changes
Checklist
Additional context