mem0 icon indicating copy to clipboard operation
mem0 copied to clipboard

SSE Connection Race Condition in MCP Server

Open fak111 opened this issue 7 months ago • 1 comments

🐛 Describe the bug

###video https://www.loom.com/share/8a15efc80194431eb9aea4e65fa1c888?sid=0818810b-fd56-4f1d-b8a8-36bcc10c940a

Description

Server-Sent Events (SSE) connection is failing due to a race condition where requests are being received before the server initialization is complete. This results in a RuntimeError being thrown with the message "Received request before initialization was complete".

Environment

  • Python Version: 3.12
  • FastAPI Version: [version]
  • Operating System: Docker Container
  • MCP Server Version: [version]

Steps to Reproduce

  1. Start the MCP server
  2. Client attempts to establish SSE connection via /mcp/cursor/sse/[username]
  3. Client sends message through /mcp/messages/?session_id=[session_id]
  4. Server fails with initialization error

Current Behavior

The server throws a RuntimeError when receiving requests before initialization is complete, resulting in failed SSE connections.

Expected Behavior

The server should either:

  1. Queue incoming requests until initialization is complete, or
  2. Return a proper 503 Service Unavailable status until ready, or
  3. Ensure initialization is complete before accepting any connections

Error Log

RuntimeError: Received request before initialization was complete

Traceback:
File "/usr/local/lib/python3.12/site-packages/mcp/server/session.py", line 147, in _receive_loop
    await super()._receive_loop()
File "/usr/local/lib/python3.12/site-packages/mcp/shared/session.py", line 370, in _receive_loop
    await self._received_request(responder)
File "/usr/local/lib/python3.12/site-packages/mcp/server/session.py", line 175, in _received_request
    raise RuntimeError("Received request before initialization was complete")

fak111 avatar May 17 '25 14:05 fak111

existing MCP issue Try restarting the client after restarting openMemory. I just had to quit Cursor and reopen and it worked.

johnwlockwood avatar May 18 '25 05:05 johnwlockwood

existing MCP issue Try restarting the client after restarting openMemory. I just had to quit Cursor and reopen and it worked.

thanks, i reopen and it work

fak111 avatar May 22 '25 08:05 fak111