Skip to content

Commit 564561f

Browse files
committed
add a cancelscope on the finally
1 parent bb24881 commit 564561f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/mcp/server/message_queue/redis.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ async def subscribe(self, session_id: UUID, callback: MessageCallback):
7070
try:
7171
yield
7272
finally:
73-
tg.cancel_scope.cancel()
74-
await self._pubsub.unsubscribe(channel) # type: ignore
75-
await self._redis.srem(self._active_sessions_key, session_id.hex)
76-
del self._session_state[session_id]
77-
logger.debug(f"Unsubscribed from Redis channel: {session_id}")
73+
with anyio.CancelScope(shield=True):
74+
tg.cancel_scope.cancel()
75+
await self._pubsub.unsubscribe(channel) # type: ignore
76+
await self._redis.srem(self._active_sessions_key, session_id.hex)
77+
del self._session_state[session_id]
78+
logger.debug(f"Unsubscribed from Redis channel: {session_id}")
7879

7980
def _extract_session_id(self, channel: str) -> UUID | None:
8081
"""Extract and validate session ID from channel."""

0 commit comments

Comments
 (0)