mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Connection closed when using same connection parameters

Open fuegoio opened this issue 3 years ago • 0 comments

Hi,

We are using MongoEngine 0.24.1 with Pymongo 4.0.0 and because of the new behavior of Pymongo 4 we have unexpected connections that are closed.

This is due to the fact that MongoEngine is re-using the Pymongo connections if the connection parameters are the same, blocking any possibility to close one connection without the other.

Example:

from mongoengine import connect, disconnect
from mongoengine import Document

class Test(Document):
         meta = {"db_alias": "db-one"}

connect(host="mongodb://test:test@localhost:27017/test", alias="db-one")
connect(host="mongodb://test:test@localhost:27017/test", alias="db-two")

# All good here
Test.objects().first()

disconnect(alias="db-two")

# The Test collection should still be accessible but is not
Test.objects().first() # Raises Exception

Exception:

  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/cursor.py", line 1248, in next
    if len(self.__data) or self._refresh():
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/cursor.py", line 1165, in _refresh
    self.__send_message(q)
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/cursor.py", line 1052, in __send_message
    response = client._run_operation(
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1267, in _run_operation
    return self._retryable_read(
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1364, in _retryable_read
    server = self._select_server(read_pref, session, address=address)
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1186, in _select_server
    topology = self._get_topology()
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1143, in _get_topology
    self._topology.open()
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/topology.py", line 188, in open
    self._ensure_opened()
  File "/home/fuego/Projects/Professional/Qobra/qobra/api/venv/lib/python3.9/site-packages/pymongo/topology.py", line 573, in _ensure_opened
    raise InvalidOperation("Cannot use MongoClient after close")
pymongo.errors.InvalidOperation: Cannot use MongoClient after close

Thanks a lot :)

fuegoio avatar Apr 13 '22 09:04 fuegoio