Skip to content

Commit 3c0501a

Browse files
author
cshinaver
committed
Remove unnecessary locks from Redis implementation
Remove redundant locks as Redis client handles thread safety internally for PubSub operations. Tests verify that the code continues to work correctly. GitHub-Issue:modelcontextprotocol#342
1 parent b42ae2c commit 3c0501a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/mcp/server/message_queue/redis.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ def __init__(
6161
self._session_state: dict[UUID, tuple[MessageCallback, TaskGroup]] = {}
6262
# Thread for pubsub listening
6363
self._pubsub_thread = None
64-
# Lock for thread safety
65-
self._lock = threading.RLock()
6664
# Ensures only one polling task runs at a time for message handling
6765
self._limiter = CapacityLimiter(1)
6866
# Active sessions set key
@@ -104,10 +102,8 @@ async def subscribe(self, session_id: UUID, callback: MessageCallback):
104102

105103
channel = self._session_channel(session_id)
106104

107-
# Use lock for thread safety
108-
with self._lock:
109-
# Subscribe to channel
110-
await anyio.to_thread.run_sync(lambda: self._pubsub.subscribe(channel))
105+
# Subscribe to channel
106+
await anyio.to_thread.run_sync(lambda: self._pubsub.subscribe(channel))
111107

112108
logger.debug(f"Subscribing to Redis channel for session {session_id}")
113109

@@ -126,10 +122,9 @@ async def subscribe(self, session_id: UUID, callback: MessageCallback):
126122
tg.cancel_scope.cancel()
127123

128124
# Unsubscribe
129-
with self._lock:
130-
await anyio.to_thread.run_sync(
131-
lambda: self._pubsub.unsubscribe(channel)
132-
)
125+
await anyio.to_thread.run_sync(
126+
lambda: self._pubsub.unsubscribe(channel)
127+
)
133128

134129
# Delete session key and remove from active sessions
135130
await anyio.to_thread.run_sync(lambda: self._redis.delete(session_key))

0 commit comments

Comments
 (0)