-
Notifications
You must be signed in to change notification settings - Fork 456
fix: use background context for MCP session and add test #406
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
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @Nerja, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue where the MCP session's connection was prematurely terminated due to its reliance on a request-scoped context. By switching to Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly addresses an issue where a cached MCP session was being closed prematurely due to using a request-scoped context. The fix of using context.Background() to initialize the session is appropriate, as the session's lifecycle should not be tied to a single request. The newly added test, TestSessionContextCancellation, effectively verifies this fix by using a spy transport to ensure the connection context is not cancelled along with the request context. The changes look good. I have one minor suggestion for improving the thread-safety of the test helper for future robustness.
| type contextSpyTransport struct { | ||
| mcp.Transport | ||
| ctx context.Context | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contextSpyTransport is not thread-safe. The ctx field is written in Connect and read in Context without synchronization. While this is safe in the current test because getSession is protected by a mutex and ensures Connect is called only once, it's a fragile pattern. If this test helper is reused in other tests with concurrent operations, it could lead to data races. Consider adding a mutex (sync.Mutex) to protect access to the ctx field to make this helper more robust.
|
I don't think there's problem with request-scoped context. Because this context is only needed to create a session. Once session object is created even if the context is cancelled it doesn't affect the session. The MCP lib doesn't store the context. @Nerja could you verify if this assumption is correct? If it is, then setting context.Background() won't solve the issue. As we'd need rather to handle stale cached session object. |
|
Closing in favor of #417 and based on #399 (comment) |
The MCP session was being initialized using the request-scoped context passed to Tools(ctx). When this context was cancelled the underlying MCP connection was closed. However, the session remained cached in the mcptoolset struct, causing subsequent calls to fail.
Changed the Connect call in getSession to use context.Background().
Added TestSessionContextCancellation to verify that the context passed to connect is not cancelled.
For questions related to executing Ping as well to confirm the status of the connection, please see #399 (comment)