Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added clean shutdown + tests #115

Merged
merged 4 commits into from
Jan 8, 2025

Conversation

jerome3o-anthropic
Copy link
Contributor

Motivation and Context

#113

When closing a StdioServerTransport, the process would hang indefinitely due to unclosed stdin/stdout streams and lingering event listeners. This was particularly noticeable in server implementations that needed to shut down cleanly. The fix ensures proper cleanup of all resources, preventing process hangs.

How Has This Been Tested?

  • Added unit test in src/server/stdio.test.ts verifying proper cleanup of stream resources and event listeners
  • Added integration test in src/integration-tests/process-cleanup.test.ts that confirms the process exits cleanly after transport closure
  • Manually verified fix using a minimal reproduction case that previously demonstrated the hang

Breaking Changes

None. This is a bug fix that maintains the existing API contract.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The fix involves three key changes in StdioServerTransport.close():

  1. Removing all event listeners, including 'drain' listeners that were previously overlooked
  2. Explicitly destroying both stdin and stdout streams
  3. Maintaining existing cleanup of the read buffer and onclose callback

The integration test was added to prevent regression, as unit tests alone didn't catch this issue due to the asynchronous nature of Node.js stream cleanup.

Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Some blocking discussion, but this is definitely important

src/server/stdio.ts Outdated Show resolved Hide resolved
src/server/stdio.ts Outdated Show resolved Hide resolved
@jspahrsummers jspahrsummers linked an issue Jan 3, 2025 that may be closed by this pull request
if (remainingDataListeners === 0) {
// Only pause stdin if we were the only listener
// This prevents interfering with other parts of the application that might be using stdin
this._stdin.pause();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised that this has any effect on termination, but then reading about Node.js readable streams, I'm like 😵

It feels a bit janky to do things this way, but I don't really have a better idea…

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per top-level PR comment, I don't see how both MCP and anything else can co-exist on the same stdin stream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly not while messages are streaming in. The only question is whether there'd ever be a reason for a server implementation to want to read from stdin after terminating the normal MCP stuff. Probably not? But it's always hard to anticipate how people will use a library.

@davidfiala
Copy link

I believe that since you are communicating a specific protocol over stdin/stdout, then it does assume you have permanently taken over both STDIO FDs.

This has interesting implications that I've had to work around. Specifically, wouldn't a console.log or console.info effectively corrupt your communication stream, if it occurs during any part of the server execution?

Here was an approximate workaround that I put at the top of servers to route the most common occurences to stderr instead:

// make console.log and console.info go to STDERR because MCP is going to take over STDOUT
const onlyStderr = new Console({ stdout: process.stderr, stderr: process.stderr });
console.info = onlyStderr.info;
console.log = onlyStderr.log;

In this case, I'd argue that yes it's fine/expected for you to end the stdin/stdout streams. It should be clearly documented that this behavior occurs IMHO. Likewise, perhaps you should hotpatch the console logs in your server.connect() on StdioServerTransport. The thing is, if the user happens to console.log BEFORE getting to your constructor, then there'd already be corruption-worthy output that the MCP client would be exposed to when it begins to read stdout. There's worse workarounds for that as well (ie, a shared sentinel to broadcast the true beginning of the MCP stream), but that is a breaking protocol change.

@jspahrsummers
Copy link
Member

This has interesting implications that I've had to work around. Specifically, wouldn't a console.log or console.info effectively corrupt your communication stream, if it occurs during any part of the server execution?

Yes, this is discussed here: https://modelcontextprotocol.io/docs/tools/debugging#server-side-logging

@Nedomas
Copy link

Nedomas commented Jan 8, 2025

Even though the fix here is for stdio specifically, I’m wondering isn’t the same leak happening in the SSE transport too?

I can’t seem to be getting rid of the active connection even with doing this:

const client = new Client({
  name: 'superinterface-mcp-client',
  version: '1.0.0',
}, {
  capabilities: {}
})

const transport =  ew SSEClientTransport(
  new URL('some-url')
)
await client.connect(transport)

await client.close()
await transport.close()

But might be an unrelated bug?

@jerome3o-anthropic
Copy link
Contributor Author

seem to be getting rid of the active connection even with doing

That's odd - likely a separate issue. Going to merge this now and follow up on the SSE bug later

@jerome3o-anthropic jerome3o-anthropic merged commit e72ff8d into main Jan 8, 2025
4 checks passed
@jerome3o-anthropic jerome3o-anthropic deleted the jerome/fix/clean-stdio-shutdown branch January 8, 2025 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to cleanly shutdown/close MCP Server
4 participants