Skip to content

fix: SSE client hangs after 30 seconds #88

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

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

mrene
Copy link
Contributor

@mrene mrene commented Mar 28, 2025

In #55, an option was added to set an sse read timeout. This timeout, however, applies to the entire readSSE function which is responsible for receiving messages from the server after the client's initialization. Because of this, any SSE client stops working 30 seconds after initialization. Any request hangs because no responses are being processed.

The original author mentioned parity with the Python SDK as a justification for adding this option. However, in the python SDK, the timeout applies to a specific call, not to the entire message loop. In Go, callers can directly create their context with a given timeout or deadline without a specific option for timeouts.

For this reason, I would recommend removing the option altogether.

Summary by CodeRabbit

  • Refactor
    • Updated real-time event stream handling by removing the configurable read timeout setting. SSE connections now conclude based on connection termination signals rather than a preset timeout, resulting in a more straightforward and consistent streaming experience.

Copy link
Contributor

coderabbitai bot commented Mar 28, 2025

Walkthrough

This pull request simplifies the SSE client setup by removing the timeout mechanism previously used to terminate read operations. The changes include removing the sseReadTimeout field from the SSEMCPClient struct and eliminating the WithSSEReadTimeout function from client initialization. Additionally, the readSSE method is updated to check for the closure of the done channel instead of using a context with a timeout, thereby altering the control flow for handling long-running SSE connections.

Changes

File(s) Changes
client/sse.go Removed the sseReadTimeout field from the SSEMCPClient struct and the WithSSEReadTimeout function. Modified NewSSEMCPClient to omit the default timeout and updated readSSE to rely on the done channel instead of a context timeout.

Possibly related PRs

✨ 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
client/sse.go (1)

1-578: Consider updating documentation to reflect the behavior change.

The removal of the timeout mechanism changes how the SSE client behaves. Consider adding a comment in the readSSE method or in the struct documentation explaining that the client now relies on the done channel for termination rather than a timeout, and that users can implement their own timeout using context if needed.

 // readSSE continuously reads the SSE stream and processes events.
-// It runs until the connection is closed or an error occurs.
+// It runs until the connection is closed, an error occurs, or the done channel is closed.
+// Users can implement their own timeout by using a context with timeout when calling Start()
+// and other methods, rather than relying on a global timeout for the SSE connection.
 func (c *SSEMCPClient) readSSE(reader io.ReadCloser) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d3dc35c and 0c405ef.

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

25-39: Removal of timeout field aligns with PR objectives.

The removal of the sseReadTimeout field from the SSEMCPClient struct is a positive change that addresses the core issue. By eliminating this field, we prevent the SSE client from hanging after 30 seconds, allowing for persistent connections as expected in SSE architecture.


57-64: Simplified client initialization without timeout configuration.

The removal of the timeout initialization in the NewSSEMCPClient function is consistent with the changes to the struct. This correctly eliminates the default 30-second timeout that was causing the client to hang.


123-127: Fixed SSE connection handling with proper termination approach.

The readSSE method now correctly checks for the closure of the done channel instead of using a context with timeout. This change ensures that the SSE reading loop continues indefinitely until explicitly terminated, which is the expected behavior for SSE connections.


41-47:

✅ Verification successful

Verify WithSSEReadTimeout function was completely removed.

I notice that while the sseReadTimeout field was removed from the struct and its initialization, we should verify that the WithSSEReadTimeout function mentioned in the summary was actually removed from the codebase.


🏁 Script executed:

#!/bin/bash
# Check if WithSSEReadTimeout function exists in the current code
echo "Checking for WithSSEReadTimeout function in client/sse.go:"
grep -n "WithSSEReadTimeout" client/sse.go || echo "Function not found (good)"

# Check if there are any references to this function elsewhere in the codebase
echo -e "\nChecking for references to WithSSEReadTimeout in other files:"
rg "WithSSEReadTimeout" --type go --files-with-matches || echo "No references found (good)"

Length of output: 487


Verification Confirmed: WithSSEReadTimeout Removal is Complete

The verification scripts confirm that the WithSSEReadTimeout function is not present in client/sse.go and no references to it exist elsewhere in the codebase. Please proceed knowing that all related removal steps have been properly implemented.

@ezynda3
Copy link
Contributor

ezynda3 commented Mar 29, 2025

I am in favor of removing this in favor of a more Go aligned method of dealing with timeouts. However, maybe it would be good to add a test or, at a minimum, some documentation about how someone could use context to handle timeouts in the README

@yp05327
Copy link

yp05327 commented Apr 10, 2025

Are there any updates? The client is borken, should be fixed ASAP.

@mrene
Copy link
Contributor Author

mrene commented Apr 10, 2025

Are there any updates? The client is borken, should be fixed ASAP.

If it helps I have a tag with the fix that you can replace in go.mod pending this being merged:

replace github.com/mark3labs/mcp-go v0.17.0 => github.com/mrene/mcp-go v0.17.0-ssefix

@ezynda3 ezynda3 merged commit f3149bf into mark3labs:main Apr 10, 2025
2 checks passed
leavez added a commit to leavez/mcp-go that referenced this pull request Apr 11, 2025
@coderabbitai coderabbitai bot mentioned this pull request Apr 15, 2025
ezynda3 pushed a commit that referenced this pull request Apr 16, 2025
* add transport layer interface

* universal client

* impl sse & stdio transport based on the original client

* refactor old client to provide compibility

* rename

* remove old client types

* add test for stdio transport

* rename 'done' to 'closed', to distinguish with ctx.Done

* add cancelSSEStream for better handling of close

* fix connection leak when start timeout

* avoid multiple starting

* use atomic for closed to be more natural compared to started

* fix leak of timer

* Create sse_test.go

* enforce test

* add comment

* sse: add custom header in start request

* update comment

* comment

* cover #88

* cover #107

* fix demo sse server in race test
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.

3 participants