File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed
Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -298,6 +298,9 @@ def disconnect(alias=DEFAULT_CONNECTION_NAME):
298298 from mongoengine import Document
299299 from mongoengine .base .common import _get_documents_by_db
300300
301+ # Check if this is an async connection BEFORE popping it
302+ was_async = is_async_connection (alias )
303+
301304 connection = _connections .pop (alias , None )
302305 if connection :
303306 # MongoEngine may share the same MongoClient across multiple aliases
@@ -306,7 +309,20 @@ def disconnect(alias=DEFAULT_CONNECTION_NAME):
306309 # Important to use 'is' instead of '==' because clients connected to the same cluster
307310 # will compare equal even with different options
308311 if all (connection is not c for c in _connections .values ()):
309- connection .close ()
312+ # Check if this was an async connection
313+ if was_async :
314+ import warnings
315+
316+ warnings .warn (
317+ f"Attempting to close async connection '{ alias } ' with sync disconnect(). "
318+ "Use disconnect_async() instead for proper cleanup." ,
319+ RuntimeWarning ,
320+ stacklevel = 2 ,
321+ )
322+ # Don't call close() on async connection to avoid the coroutine warning
323+ # The connection will be closed when the event loop is cleaned up
324+ else :
325+ connection .close ()
310326
311327 if alias in _dbs :
312328 # Detach all cached collections in Documents
You can’t perform that action at this time.
0 commit comments