Skip to content

Commit 7a07dc1

Browse files
committed
<FIX>: async support for disconnect
1 parent 5a370b6 commit 7a07dc1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

mongoengine/connection.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)