fix: ensure Stdio transport threads are daemons and closed properly (…#869
Open
noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: ensure Stdio transport threads are daemons and closed properly (…#869noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…odelcontextprotocol#759) - Use Schedulers.newSingle with daemon=true for StdioClientTransport and StdioServerTransportProvider. - Explicitly close process streams in StdioClientTransport#closeGracefully to unblock reading threads. - Removes unused Executors imports. This prevents Stdio transport threads from lingering and blocking JVM exit. Closes modelcontextprotocol#759
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
This PR ensures that all threads created by
StdioClientTransportandStdioServerTransportProviderare daemon threads and are properly unblocked during shutdown. It also improves the graceful closure logic for the server process.Motivation and Context
Fixes #759.
In the previous implementation,
StdioClientTransportusedExecutors.newSingleThreadExecutor()with default thread factories, resulting in non-daemon threads. These threads would linger indefinitely after the transport was closed—especially Windows where they might be blocked on native I/O—preventing the JVM from exiting naturally.The changes solve this by:
Schedulers.newSingle(name, true)to ensure all transport schedulers use daemon threads.InputStream,ErrorStream, andOutputStreamduring shutdown to unblock any threads waiting onreadLine()orwrite().How Has This Been Tested?
Tested on Linux (Ubuntu) using Java 17:
closeGracefully().pool-x-thread-ywould linger as non-daemons.StdioMcpAsyncClientTests,StdioMcpAsyncServerTests, etc.) to ensure protocol integrity is maintained. (162 tests passed).Breaking Changes
None. This is a behavioral fix for internal resource management.
Types of changes
Checklist
Additional context
The use of
Schedulers.newSingle(name, true)is more idiomatic for Reactor-based applications than manually wrapping executors viaSchedulers.fromExecutorService, as it handles both naming and daemon status natively while ensuring proper cleanup whendispose()is called.