Skip to content
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

ignore the ResourceWarnings before they become unraisable exceptions #6872

Merged
merged 4 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/6872.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed suppression of :py:class:`ResourceWarning`s in the pytest setup -- by :user:`graingert`.
10 changes: 5 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ addopts =
filterwarnings =
error
ignore:module 'ssl' has no attribute 'OP_NO_COMPRESSION'. The Python interpreter is compiled against OpenSSL < 1.0.0. Ref. https.//docs.python.org/3/library/ssl.html#ssl.OP_NO_COMPRESSION:UserWarning
ignore:Exception ignored in. <function _SSLProtocolTransport.__del__ at.:pytest.PytestUnraisableExceptionWarning:_pytest.unraisableexception
ignore:Exception ignored in. <coroutine object BaseConnector.close at 0x.:pytest.PytestUnraisableExceptionWarning:_pytest.unraisableexception
ignore:Exception ignored in. <coroutine object ClientSession._request at 0x.:pytest.PytestUnraisableExceptionWarning:_pytest.unraisableexception
ignore:Exception ignored in. <function ClientSession.__del__ at 0x.:pytest.PytestUnraisableExceptionWarning:_pytest.unraisableexception
ignore:Exception ignored in. <_io.FileIO .closed.>:pytest.PytestUnraisableExceptionWarning:_pytest.unraisableexception
ignore:unclosed transport <asyncio.sslproto._SSLProtocolTransport object at 0x.*:ResourceWarning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graingert any suggestions on how to make this scoped to a single module?

Copy link
Contributor Author

@graingert graingert Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to talk to @vstinner on how to get per module resource warnings

You can dig out the original owner of the object via tracemalloc but it slows down python considerably, The issue being tracemalloc is great and gives you all the info you need, but it's too slow to run all the time, also resources warnings are flakey which means by the time you see one it's too late and you weren't running tracemalloc

What I think should happen is that:

  • When any object with a custom __del__ is being constructed
  • If the ResourceWarning filter is configured as "error"
  • the frame module, filename and line number 2 frames above the last __init__ call is recorded, as a sort of tracemalloc of last-resort

Then when the ResourceWarning is raised it's associated with those lines

ignore:coroutine 'BaseConnector.close' was never awaited:RuntimeWarning
ignore:coroutine 'ClientSession._request' was never awaited:RuntimeWarning
ignore:Unclosed client session <aiohttp.client.ClientSession object at 0x:ResourceWarning
ignore:unclosed file <_io.FileIO.*:ResourceWarning
ignore:The loop argument is deprecated:DeprecationWarning:asyncio
ignore:Creating a LegacyVersion has been deprecated and will be removed in the next major release:DeprecationWarning::
# The following deprecation warning is triggered by importing
Expand Down
22 changes: 11 additions & 11 deletions tests/test_run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@

_has_unix_domain_socks = hasattr(socket, "AF_UNIX")
if _has_unix_domain_socks:
_abstract_path_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
_abstract_path_sock.bind(b"\x00" + uuid4().hex.encode("ascii"))
except FileNotFoundError:
_abstract_path_failed = True
else:
_abstract_path_failed = False
finally:
_abstract_path_sock.close()
del _abstract_path_sock
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as _abstract_path_sock:
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
try:
_abstract_path_sock.bind(b"\x00" + uuid4().hex.encode("ascii"))
except FileNotFoundError:
_abstract_path_failed = True
else:
_abstract_path_failed = False
finally:
del _abstract_path_sock
else:
_abstract_path_failed = True

Expand All @@ -48,7 +47,8 @@
# support, but the target system still may not have it.
# So let's ensure that we really have IPv6 support.
try:
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM):
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
pass
except OSError:
HAS_IPV6 = False

Expand Down
3 changes: 2 additions & 1 deletion tests/test_tcp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# support, but the target system still may not have it.
# So let's ensure that we really have IPv6 support.
try:
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM):
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
pass
except OSError:
has_ipv6 = False

Expand Down