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

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Aug 6, 2022

What do these changes do?

@webknjaz:matrix.org btw you're using PytestUnraisableExceptionWarning incorrectly - you need to ignore the ResourceWarning not the PytestUnraisableExceptionWarning. Because pytest will only collect one unraisable per test and if you allow a ResourceWarning to raise then you defer the cleanup of the underlying resource eg:

_warn("...", ResourceWarning)  # this raises 
self._sock.close()  # this never gets called

Are there changes in behavior for the user?

nope

Related issue number

#6872

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

setup.cfg Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Aug 6, 2022

Codecov Report

Merging #6872 (43c983e) into master (013e45f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 43c983e differs from pull request most recent head d911fa7. Consider uploading reports for the commit d911fa7 to get more accurate results

@@           Coverage Diff           @@
##           master    #6872   +/-   ##
=======================================
  Coverage   93.44%   93.44%           
=======================================
  Files         104      104           
  Lines       30629    30630    +1     
  Branches     3076     3079    +3     
=======================================
+ Hits        28621    28622    +1     
  Misses       1838     1838           
  Partials      170      170           
Flag Coverage Δ
unit 93.36% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tests/test_run_app.py 86.64% <100.00%> (ø)
tests/test_tcp_helpers.py 84.61% <100.00%> (+0.30%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@webknjaz webknjaz merged commit 799dc87 into aio-libs:master Aug 8, 2022
@patchback
Copy link
Contributor

patchback bot commented Aug 8, 2022

Backport to 3.9: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 799dc87 on top of patchback/backports/3.9/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872

Backporting merged PR #6872 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.9/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872 upstream/3.9
  4. Now, cherry-pick PR ignore the ResourceWarnings before they become unraisable exceptions #6872 contents into that branch:
    $ git cherry-pick -x 799dc8742e2cc0b5a819ba454878d09bbfe2df82
    If it'll yell at you with something like fatal: Commit 799dc8742e2cc0b5a819ba454878d09bbfe2df82 is a merge but no -m option was given., add -m 1 as follows intead:
    $ git cherry-pick -m1 -x 799dc8742e2cc0b5a819ba454878d09bbfe2df82
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR ignore the ResourceWarnings before they become unraisable exceptions #6872 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.9/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@webknjaz webknjaz removed the bot:chronographer:skip This PR does not need to include a change note label Aug 8, 2022
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Aug 8, 2022
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

webknjaz added a commit that referenced this pull request Aug 8, 2022
This change updates the pytest configuration to defeat the
unintentional resource leaks caused by an incorrect method of
suppressing the `PytestUnraisableExceptionWarning`s.

Contributed by Thomas Grainger.

(cherry picked from commit 799dc87)
@webknjaz
Copy link
Member

webknjaz commented Aug 8, 2022

Backported as e4266b1 + 8201922.

@graingert graingert deleted the patch-2 branch August 9, 2022 06:33
webknjaz added a commit that referenced this pull request Sep 7, 2022
This change updates the pytest configuration to defeat the
unintentional resource leaks caused by an incorrect method of
suppressing the `PytestUnraisableExceptionWarning`s.

Contributed by Thomas Grainger.

(cherry picked from commit 799dc87)
@webknjaz
Copy link
Member

webknjaz commented Sep 7, 2022

Also backported to 3.8 as 4d10cc9

@patchback
Copy link
Contributor

patchback bot commented Sep 10, 2022

Backport to 3.8: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 799dc87 on top of patchback/backports/3.8/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872

Backporting merged PR #6872 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.8/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872 upstream/3.8
  4. Now, cherry-pick PR ignore the ResourceWarnings before they become unraisable exceptions #6872 contents into that branch:
    $ git cherry-pick -x 799dc8742e2cc0b5a819ba454878d09bbfe2df82
    If it'll yell at you with something like fatal: Commit 799dc8742e2cc0b5a819ba454878d09bbfe2df82 is a merge but no -m option was given., add -m 1 as follows intead:
    $ git cherry-pick -m1 -x 799dc8742e2cc0b5a819ba454878d09bbfe2df82
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR ignore the ResourceWarnings before they become unraisable exceptions #6872 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.8/799dc8742e2cc0b5a819ba454878d09bbfe2df82/pr-6872
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

webknjaz added a commit that referenced this pull request Sep 10, 2022
This change updates the pytest configuration to defeat the
unintentional resource leaks caused by an incorrect method of
suppressing the `PytestUnraisableExceptionWarning`s.

Contributed by Thomas Grainger.

(cherry picked from commit 799dc87)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants