mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

Fix: fix client sse tcp connection re-use by draining outstanding io

Open bcain99 opened this issue 7 months ago • 1 comments

Fix: fix client sse tcp connection re-use by draining outstanding io

This is a very targeted fix that does not introduce any additional functionality but fixes an issue with SSE TCP connection reuse from the mcp-go client.

Root cause: Go http client will not re-use connection if a close happens and outstanding io has not been drained

All tests pass

Note: It is hard to write a test case for this as you will need to track source port numbers to look for re-use

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of server responses to ensure more reliable processing and error reporting.

bcain99 avatar Apr 26 '25 14:04 bcain99

Walkthrough

The change updates the handling of HTTP response bodies in the SendRequest method within the SSE transport client. Instead of deferring the closure of the response body immediately after the request, the code now reads the entire response body into memory using io.ReadAll, closes the body right after reading, and uses the read content for error reporting. This prevents redundant reads and ensures the body is fully drained before closure.

Changes

Files Change Summary
client/transport/sse.go Modified SendRequest to read the entire HTTP response body before closing it and updated error reporting to use the read content, removing redundant body reads.

Possibly related PRs

  • mark3labs/mcp-go#206: Addresses cleanup of the response channel to prevent resource leaks within the same SendRequest method, focusing on a different aspect of resource management.

📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33c98f164515ab4185524c23015f9e4eb9499dcd and c3e3819ac738823d26be9e8ec0851eb0fa9ff491.

📒 Files selected for processing (1)
  • client/transport/sse.go (1 hunks)
🔇 Additional comments (1)
client/transport/sse.go (1)

281-288: Great fix for TCP connection reuse!

This change correctly addresses the issue where TCP connections weren't being reused because of outstanding IO data. By explicitly reading the entire response body with io.ReadAll() and immediately closing it, you ensure that all data is drained from the connection before it's returned to the connection pool.

The fix aligns with Go's HTTP client behavior, which requires response bodies to be fully read and closed for connection reuse. This should result in better performance by reducing the number of TCP connections created over time.

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 26 '25 14:04 coderabbitai[bot]