[go: nahoru, domu]

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

gh-121084: Fix test_typing random leaks #121360

Merged
merged 2 commits into from
Jul 4, 2024
Merged

Conversation

vstinner
Copy link
Member
@vstinner vstinner commented Jul 4, 2024

Clear typing ABC caches when running tests for refleaks (-R option): call _abc_caches_clear() on typing abstract classes and their subclasses.

Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
@vstinner
Copy link
Member Author
vstinner commented Jul 4, 2024

Without this change, test_typing leaks randomly. Examples of output:

  • test_typing leaked [0, 0, 1] memory blocks, sum=1 (this is fine)
  • test_typing leaked [2, 0, 0] memory blocks, sum=2 (this is fine)
  • test_typing leaked [1, 1, 0] memory blocks, sum=2 (this is fine)

With this change, it no longer leaks.

cc @sobolevn @JelleZijlstra @AlexWaygood @Eclips4

Copy link
Member
@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Do we have any idea why typing is particularly affected here? It's it because of some unusual things we're doing with ABCs in the typing module, or is it a general issue with ABC caches? If the latter, maybe we should be doing this for every module, not just typing?

Lib/test/libregrtest/utils.py Outdated Show resolved Hide resolved
@vstinner
Copy link
Member Author
vstinner commented Jul 4, 2024

Do we have any idea why typing is particularly affected here?

test_typing creates many classes, register them in ABC, check for subclasses, etc. It uses heavily the ABC caches.

@AlexWaygood
Copy link
Member

So do we think that this is something that could happen to any module that has a lot of ABCs in it? If we added a few more ABCs to collections.abc, could it start happening to that module as well? Or do we think there's something specific about what typing is doing in e.g. the Protocol implementation that's causing this?

Copy link
Member
@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I'm fine with this if it fixes the tests, but I'd prefer it if we got to the bottom of what the root cause is here. If it's something that could affect any module with lots of ABCs, then that suggests a more general fix might be warranted. If it's something that specifically affects typing because of something we're doing in that module, then we could probably do a more targeted fix.

@vstinner vstinner merged commit 5f660e8 into python:main Jul 4, 2024
43 checks passed
@vstinner vstinner deleted the test_typing branch July 4, 2024 17:38
@vstinner
Copy link
Member Author
vstinner commented Jul 4, 2024

I'm fine with this if it fixes the tests, but I'd prefer it if we got to the bottom of what the root cause is here. If it's something that could affect any module with lots of ABCs, then that suggests a more general fix might be warranted. If it's something that specifically affects typing because of something we're doing in that module, then we could probably do a more targeted fix.

Refleak check is both strict and stupid. It counts beans. If a function allocates one memory block and doesn't release it, refleak check reports a memory leak. It doesn't understand if the memory block belongs to a cache or if it's "a real leak".

That's why we need some workarounds like that which are an exhaustive list of caches that should be cleared after after run.

I don't think that anything is wrong with typing or test_typing. They are populating caches, that's good. Internals rely on the garbage collector and weak references which make test_typing a little bit less deterministic when it comes to "refleak check".

Ah, there is one thing that I noticed. It seems like the ABC cache is a set of weak references, but dead references are never removed from the set. That's one thing that could be enhanced and make this problem less important.

@vstinner vstinner added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Jul 4, 2024
@miss-islington-app
Copy link

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 4, 2024
Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
(cherry picked from commit 5f660e8)

Co-authored-by: Victor Stinner <vstinner@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 4, 2024
Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
(cherry picked from commit 5f660e8)

Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link
bedevere-app bot commented Jul 4, 2024

GH-121372 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Jul 4, 2024
@bedevere-app
Copy link
bedevere-app bot commented Jul 4, 2024

GH-121373 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 4, 2024
vstinner added a commit that referenced this pull request Jul 4, 2024
gh-121084: Fix test_typing random leaks (GH-121360)

Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
(cherry picked from commit 5f660e8)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Jul 4, 2024
gh-121084: Fix test_typing random leaks (GH-121360)

Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
(cherry picked from commit 5f660e8)

Co-authored-by: Victor Stinner <vstinner@python.org>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Clear typing ABC caches when running tests for refleaks (-R option):
call _abc_caches_clear() on typing abstract classes and their
subclasses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants