python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Error during cleanup of server sqlite: Attempted to exit cancel scope in a different task than it was entered in

Open xiongzhp opened this issue 1 year ago • 8 comments

Describe the bug When running examples\clients\simple-chatbot\mcp_simple_chatbot\main.py

2025-03-10 19:55:12,391 - ERROR - Error during cleanup of server sqlite: Attempted to exit cancel scope in a different task than it was entered in 2025-03-10 19:55:12,467 - ERROR - an error occurred during closing of asynchronous generator <async_generator object stdio_client at 0x0000014698D39930>

Expected behavior A clear and concise description of what you expected to happen.

Desktop (please complete the following information):

  • OS: win10 and ubuntu

xiongzhp avatar Mar 10 '25 12:03 xiongzhp

I got the same error.

This is the only example I've seen of using python to manage multiple mcp servers (and it doesn't seem to work). Are there others?

jritsema avatar Mar 12 '25 02:03 jritsema

having the same issue.

immediately after receiving an OK response from the LLM, get this error.

EDIT: from Claude: The key problem is a violation of a fundamental principle in asynchronous programming. In async frameworks (like Python's asyncio, trio, or curio), a "cancel scope" is a context that manages cancellation of operations. This error indicates the code is trying to exit a cancel scope from a different task than the one where it was created.

I may have a solution but I won't be able to test it until about 6 hours time as i have to go out.

Fingers crossed. but the solution is from Claude. It's already sent me on a number of wild goose chases w/r/t this exact issue. but i have a better understanding of it now so i should be able to better analyze the proposed fix line by line once i attempt to implement it.

I'll keep you posted

s-konnex-engine avatar Mar 12 '25 14:03 s-konnex-engine

I met the same problem, how did you solve it?

WikiHkz avatar Apr 01 '25 08:04 WikiHkz

having the same issue.

immediately after receiving an OK response from the LLM, get this error.

EDIT: from Claude: The key problem is a violation of a fundamental principle in asynchronous programming. In async frameworks (like Python's asyncio, trio, or curio), a "cancel scope" is a context that manages cancellation of operations. This error indicates the code is trying to exit a cancel scope from a different task than the one where it was created.

I may have a solution but I won't be able to test it until about 6 hours time as i have to go out.

Fingers crossed. but the solution is from Claude. It's already sent me on a number of wild goose chases w/r/t this exact issue. but i have a better understanding of it now so i should be able to better analyze the proposed fix line by line once i attempt to implement it.

I'll keep you posted

Many 6 hours have passed now, are you back?

WikiHkz avatar Apr 01 '25 08:04 WikiHkz

asyncio.gather() cannot be used since the order of initialization is unknown. we need to know this initialization order since we need to close the servers in reverse initialization order. let me know if there is a way to launch the servers in parallel, since this is a big bottleneck seemingly.

Broyojo avatar Apr 01 '25 14:04 Broyojo

I tried to modify the code without asyncio.gather(), I got successful.

https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py#L283

I tried to change below

    async def cleanup_servers(self) -> None:
        """Clean up all servers properly."""
        for server in self.servers:
            await server.cleanup()

I don't know this way is good or not, so I tell you informations but I don't create PR.

dasoran avatar Apr 08 '25 16:04 dasoran

yeah this should work, but it is slow since you have to initialize and close the servers in a serial manner

Broyojo avatar Apr 08 '25 18:04 Broyojo

I got the same error.

wenxuwan avatar May 14 '25 03:05 wenxuwan