Skip to content

fix(mcp): prevent panic when SSE session is missing#2583

Closed
vishnukannaujia wants to merge 2 commits intogoogleapis:mainfrom
vishnukannaujia:fix-2548-ssemanager-get-nil-panic
Closed

fix(mcp): prevent panic when SSE session is missing#2583
vishnukannaujia wants to merge 2 commits intogoogleapis:mainfrom
vishnukannaujia:fix-2548-ssemanager-get-nil-panic

Conversation

@vishnukannaujia
Copy link

Summary

  • fix sseManager.get to only update lastActive when the session exists
  • return (nil, false) cleanly for unknown sessionId lookups
  • add a regression test for unknown session retrieval

Why

Issue #2548 reports a nil pointer dereference in sseManager.get() when sessionId is unknown. The map lookup can return nil, and unguarded field assignment panics.

Fix

Guard the lastActive update behind ok, preserving existing behavior for valid sessions and avoiding panic for invalid IDs.

Testing

  • Added TestSseManagerGetUnknownSession in internal/server/mcp_test.go
  • Local go test could not run in this environment because go is not installed (zsh: command not found: go)

Fixes #2548

@google-cla
Copy link

google-cla bot commented Feb 28, 2026

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 addresses a critical bug in the SSE session manager that could lead to a nil pointer dereference when attempting to retrieve a non-existent session. The changes enhance the robustness of session handling by safely managing unknown session lookups and include a dedicated test to ensure the stability of this behavior.

Highlights

  • Nil Pointer Dereference Fix: Implemented a guard in sseManager.get to prevent a nil pointer dereference when attempting to update lastActive for a non-existent SSE session.
  • Robust Session Retrieval: Ensured that sseManager.get now correctly returns (nil, false) for unknown session IDs, maintaining existing behavior for valid sessions.
  • Regression Test Added: Introduced a new test case, TestSseManagerGetUnknownSession, to validate the fix and prevent future regressions for unknown session lookups.
Changelog
  • internal/server/mcp.go
    • Guarded the session.lastActive update with an if ok check to prevent panics when a session is not found.
  • internal/server/mcp_test.go
    • Added TestSseManagerGetUnknownSession to verify that sseManager.get correctly handles requests for non-existent sessions by returning (nil, false).
Activity
  • The author added a regression test for unknown session retrieval.
  • The author noted that local go test could not be run due to go not being installed in their environment.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request correctly fixes a potential panic in sseManager.get by adding a check before accessing a potentially nil session. The accompanying regression test is also well-written and covers the fixed scenario. The included suggestion aims to slightly improve the test's failure diagnostics.

Comment on lines +1179 to +1184
if ok {
t.Fatal("expected unknown session lookup to return ok=false")
}
if session != nil {
t.Fatal("expected unknown session lookup to return nil session")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better test failure diagnostics, you could combine the checks for the two return values. This provides a more comprehensive error message if the test fails, showing both returned values at once.

Suggested change
if ok {
t.Fatal("expected unknown session lookup to return ok=false")
}
if session != nil {
t.Fatal("expected unknown session lookup to return nil session")
}
if ok || session != nil {
t.Fatalf("manager.get() with unknown id returned (%v, %v), want (nil, false)", session, ok)
}

@vishnukannaujia
Copy link
Author

Addressed review feedback from Gemini:

  • updated TestSseManagerGetUnknownSession assertion to report both return values in a single diagnostic

Change pushed to the same branch: fix-2548-ssemanager-get-nil-panic (commit c7059bc229).

I will proceed with any additional maintainer feedback once CLA is complete.

@averikitsch
Copy link
Contributor

Thanks for the contribution. We have merged this fix in https://github.com/googleapis/genai-toolbox/pull/2557/changes. Thanks again!

@averikitsch averikitsch closed this Mar 2, 2026
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.

Bug: sseManager.get() dereferences nil session pointer — panics on unknown sessionId

3 participants