-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3193 - Add ResourceWarning for unclosed MongoClients in __del__ #1833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d7d8ce8
f5fbeef
2bac24c
c9f99a0
928b164
3c3e305
f8ef260
bf6868a
9f3bd1d
7a6ba04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
|
||
import contextlib | ||
import os | ||
import warnings | ||
import weakref | ||
from collections import defaultdict | ||
from typing import ( | ||
|
@@ -864,6 +865,7 @@ def __init__( | |
) | ||
|
||
self._opened = False | ||
self._closed = False | ||
self._init_background() | ||
|
||
if _IS_SYNC and connect: | ||
|
@@ -1173,6 +1175,22 @@ def __getitem__(self, name: str) -> database.AsyncDatabase[_DocumentType]: | |
""" | ||
return database.AsyncDatabase(self, name) | ||
|
||
def __del__(self) -> None: | ||
"""Check that this AsyncMongoClient has been closed and issue a warning if not.""" | ||
# TODO: Remove in https://jira.mongodb.org/browse/PYTHON-4731 | ||
try: | ||
if not self._closed: | ||
warnings.warn( | ||
f"Unclosed {self}", | ||
|
||
ResourceWarning, | ||
stacklevel=2, | ||
source=self, | ||
) | ||
if _IS_SYNC and self._opened: | ||
self.close() # type: ignore[unused-coroutine] | ||
|
||
except AttributeError: | ||
pass | ||
|
||
def _close_cursor_soon( | ||
self, | ||
cursor_id: int, | ||
|
@@ -1540,6 +1558,7 @@ async def close(self) -> None: | |
if self._encrypter: | ||
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened. | ||
await self._encrypter.close() | ||
self._closed = True | ||
|
||
if not _IS_SYNC: | ||
# Add support for contextlib.aclosing. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize we removed
MongoClient.__del__
. I think we should preserve the 4.8 behavior and changeconnect=False
and remove the__del__
behavior together in 5.0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't we want to keep the
__del__
warning forever?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, we never had a
__del__
. Yes, I agree.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this comment now.