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

AsyncMongoClient connection error causes UnboundLocalError

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.12.1
    • Affects Version/s: 4.12
    • Component/s: None
    • None
    • None
    • Python Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Bug in our async protocol implementation. When I kill the secondary mongod this error is displayed in pymongo by the minPoolSize task:

      MongoDB: 8.0.4, PyMongo: 4.13.0.dev0, Python 3.13.0 (v3.13.0:60403a5409f, Oct  7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]
      MongoClient background task encountered an error:
      Traceback (most recent call last):
        File "/Users/shane/git/mongo-python-driver/pymongo/pool_shared.py", line 342, in _configured_protocol_interface
          transport, protocol = await asyncio.get_running_loop().create_connection(  # type: ignore[call-overload]
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ...<4 lines>...
          )
          ^
        File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/base_events.py", line 1182, in create_connection
          transport, protocol = await self._create_connection_transport(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ...<2 lines>...
              ssl_shutdown_timeout=ssl_shutdown_timeout)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/base_events.py", line 1215, in _create_connection_transport
          await waiter
        File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/sslproto.py", line 578, in _on_handshake_complete
          raise handshake_exc
      ConnectionResetError
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/Users/shane/git/mongo-python-driver/pymongo/asynchronous/mongo_client.py", line 2212, in _process_periodic_tasks
          await self._topology.update_pool()
        File "/Users/shane/git/mongo-python-driver/pymongo/asynchronous/topology.py", line 691, in update_pool
          await server.pool.remove_stale_sockets(generation)
        File "/Users/shane/git/mongo-python-driver/pymongo/asynchronous/pool.py", line 1003, in remove_stale_sockets
          conn = await self.connect()
                 ^^^^^^^^^^^^^^^^^^^^
        File "/Users/shane/git/mongo-python-driver/pymongo/asynchronous/pool.py", line 1057, in connect
          networking_interface = await _configured_protocol_interface(self.address, self.opts)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/shane/git/mongo-python-driver/pymongo/pool_shared.py", line 354, in _configured_protocol_interface
          transport.abort()
          ^^^^^^^^^
      UnboundLocalError: cannot access local variable 'transport' where it is not associated with a value
      

      Right the initial connection fails because I shutdown the server, the bug is UnboundLocalError.

      Introduced in PyMongo 4.12 in PYTHON-4493.

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: