From 7c7a76db5146c40ca34db77aa33846b8b6b0bfab Mon Sep 17 00:00:00 2001 From: RichardoMrMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Sat, 17 Oct 2026 17:27:31 +0800 Subject: [PATCH 1/2] fix(mcp): detach session context instead of leaving it attached _safe_detach inverted the hasattr guard: it returned early when the session context was already attached, so the attribute was never removed after the client transport context exited. A reused read stream would carry stale session attributes (server address/port, session id) into the next connection. Measured on main (0da676ca): after attach then detach, _session_context remained present; after the fix it is removed. Regression tests cover the attached, unattached, and None paths. Assisted-by: Doubao --- .../instrumentation/mcp/session_handler.py | 2 +- .../tests/test_session_handler.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/session_handler.py b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/session_handler.py index 27915f608..4d88415f4 100644 --- a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/session_handler.py +++ b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/src/opentelemetry/instrumentation/mcp/session_handler.py @@ -82,7 +82,7 @@ def _safe_attach(self, context: Any): def _safe_detach(self, context: Any): try: - if context is None or hasattr(context[0], "_session_context"): + if context is None or not hasattr(context[0], "_session_context"): return delattr(context[0], "_session_context") except Exception: diff --git a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/tests/test_session_handler.py b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/tests/test_session_handler.py index 205b2531a..491c352d0 100644 --- a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/tests/test_session_handler.py +++ b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/tests/test_session_handler.py @@ -14,6 +14,7 @@ import os from contextlib import asynccontextmanager +from types import SimpleNamespace import pytest from mcp import JSONRPCRequest @@ -25,6 +26,7 @@ from opentelemetry import context, propagate from opentelemetry import trace as trace_api from opentelemetry.instrumentation.mcp import MCPInstrumentor, ServerMetrics +from opentelemetry.instrumentation.mcp.session_handler import _SessionContext from opentelemetry.metrics import get_meter from opentelemetry.trace import SpanKind @@ -38,6 +40,25 @@ async def mock_client(params): yield params +def test_session_context_detach_removes_attached_context(): + target = SimpleNamespace() + session_context = _SessionContext({}) + context = (target,) + session_context._safe_attach(context) + assert hasattr(target, "_session_context") + + session_context._safe_detach(context) + + assert not hasattr(target, "_session_context") + + +def test_session_context_detach_without_attachment_does_not_raise(): + target = SimpleNamespace() + session_context = _SessionContext({}) + session_context._safe_detach((target,)) + assert not hasattr(target, "_session_context") + + @pytest.mark.asyncio async def test_client_invalid(tracer_provider): import mcp.client.sse # noqa: PLC0415 From beb9e81511b9536933b02c8bd55ae9d2275f9a03 Mon Sep 17 00:00:00 2001 From: RichardoMrMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Sat, 17 Oct 2026 17:29:00 +0800 Subject: [PATCH 2/2] changelog: add #280 MCP session detach fix Assisted-by: Doubao --- .../loongsuite-instrumentation-mcp/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/CHANGELOG.md b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/CHANGELOG.md index 2d1bef2cd..1cebb5491 100644 --- a/instrumentation-loongsuite/loongsuite-instrumentation-mcp/CHANGELOG.md +++ b/instrumentation-loongsuite/loongsuite-instrumentation-mcp/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- Detach the MCP session context instead of leaving it attached after the client transport exits + ([#280](https://github.com/alibaba/loongsuite-python/pull/280)) + ## Version 0.9.0 (2026-09-07) There are no changelog entries for this release.