Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-5133

PyMongo Async Client Breaksdown with " <pymongo._asyncio_lock.Condition object at... [locked]> is bound to a different event loop"

    • None
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Detailed steps to reproduce the problem?

      I'm not sure how to reproduce the problem. We are using Uvicorn with multiple workers. In the stacktrace you can see we are async iterating over a cursor, while the errors caused in multiple loops trying to access the asyncio loop to perform a lock

       

      Traceback (most recent call last):
        File "/usr/local/lib/python3.12/site-packages/anyio/streams/memory.py", line 111, in receive
          return self.receive_nowait()
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/anyio/streams/memory.py", line 106, in receive_nowait
          raise WouldBlock
      anyio.WouldBlock
      During handling of the above exception, another exception occurred:
      Traceback (most recent call last):
       
      
        File "/usr/local/lib/python3.12/site-packages/anyio/streams/memory.py", line 124, in receive
          return receiver.item
                 ^^^^^^^^^^^^^
      AttributeError: 'MemoryObjectItemReceiver' object has no attribute 'item'
      During handling of the above exception, another exception occurred:
      Traceback (most recent call last):
        File "/usr/local/lib/python3.12/site-packages/starlette/middleware/base.py", line 157, in call_next
          message = await recv_stream.receive()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/anyio/streams/memory.py", line 126, in receive
          raise EndOfStream
      anyio.EndOfStream
      During handling of the above exception, another exception occurred:
      Traceback (most recent call last):
        File "/opt/loora/remote_configs/routers/middleware.py", line 121, in dispatch
          response = await call_next(request)
                     ^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/starlette/middleware/base.py", line 163, in call_next
          raise app_exc
        File "/usr/local/lib/python3.12/site-packages/starlette/middleware/base.py", line 149, in coro
          await self.app(scope, receive_or_disconnect, send_no_error)
        File "/usr/local/lib/python3.12/site-packages/fastapi_injector/request_scope.py", line 177, in __call__
          await self.app(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
          await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
          raise exc
        File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
          await app(scope, receive, sender)
        File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 715, in __call__
          await self.middleware_stack(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 735, in app
          await route.handle(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
          await self.app(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 76, in app
          await wrap_app_handling_exceptions(app, request)(scope, receive, send)
        File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
          raise exc
        File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
          await app(scope, receive, sender)
        File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
          response = await f(request)
                     ^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/fastapi/routing.py", line 301, in app
          raw_response = await run_endpoint_function(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/fastapi/routing.py", line 212, in run_endpoint_function
          return await dependant.call(**values)
      
      ...
      ...
      ...
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/collection.py", line 1756, in find_one
          async for result in cursor.limit(-1):
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/cursor.py", line 1283, in __anext__
          return await self.next()
                 ^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/cursor.py", line 1259, in next
          if len(self._data) or await self._refresh():
                                ^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/cursor.py", line 1207, in _refresh
          await self._send_message(q)
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/cursor.py", line 1102, in _send_message
          response = await client._run_operation(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/_csot.py", line 109, in csot_wrapper
          return await func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 1758, in _run_operation
          return await self._retryable_read(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 1867, in _retryable_read
          return await self._retry_internal(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/_csot.py", line 109, in csot_wrapper
          return await func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 1823, in _retry_internal
          return await _ClientConnectionRetryable(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 2579, in run
          return await self._read() if self._is_read else await self._write()
                 ^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 2714, in _read
          self._server = await self._get_server()
                         ^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 2670, in _get_server
          return await self._client._select_server(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line 1653, in _select_server
          server = await topology.select_server(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line 402, in select_server
          server = await self._select_server(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line 380, in _select_server
          servers = await self.select_servers(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line 287, in select_servers
          server_descriptions = await self._select_servers_loop(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line 361, in _select_servers_loop
          await _async_cond_wait(self._condition, common.MIN_HEARTBEAT_INTERVAL)
        File "/usr/local/lib/python3.12/site-packages/pymongo/lock.py", line 86, in _async_cond_wait
          return await wait_for(condition.wait(), timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
          return await fut
                 ^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/_asyncio_lock.py", line 226, in wait
          fut = self._get_loop().create_future()
                ^^^^^^^^^^^^^^^^
        File "/usr/local/lib/python3.12/site-packages/pymongo/_asyncio_lock.py", line 28, in _get_loopא
          raise RuntimeError(f"{self!r} is bound to a different event loop")
      RuntimeError: <pymongo._asyncio_lock.Condition object at 0x7f0012875550 [locked]> is bound to a different event loop
      

      The exact Python version used, with patch level:

       

      
      

      3.12.8

       

      The exact version of PyMongo used, with patch level:

       

      4.11
      True

       

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      We are using an Atlas hosted MongoDB cluster with 1 master and 2 replicas (3 nodes)  

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Debian Bullseye based Docker Image, on top of amazon linux AMI 

      Web framework or asynchronous network library used, if any, with version

      fast 0.115.8 + uvicorn 0.34.0

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            idan@loora.ai Idan Sheinberg
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None