gh-152753: Fix memory leak in ipaddress properties by switching to cached_property#152790
Open
bastitva0-blip wants to merge 5 commits into
Open
gh-152753: Fix memory leak in ipaddress properties by switching to cached_property#152790bastitva0-blip wants to merge 5 commits into
bastitva0-blip wants to merge 5 commits into
Conversation
… to cached_property
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
f95478c to
a1fd3d8
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Author
|
All 49 checks have successfully passed! The PR is fully updated with the base branch and ready for review whenever a maintainer has some time. Thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #152753
Description
Using
@functools.lru_cache()on instance methods creates a global cache that retains references toself. This keeps the instances alive indefinitely and prevents proper garbage collection, creating a memory leak.This PR resolves the issue by replacing the
lru_cachedecorators with@functools.cached_propertyon the affected lookup properties withinLib/ipaddress.py. This correctly ties the cache lifetime to the lifecycle of the instance itself.Changes
IPv4Address.is_private,IPv4Address.is_global,IPv4Network.is_global, andIPv6Address.is_private.'__dict__'to__slots__insideIPv4Addressso thatcached_propertyhas an active instance dictionary to store its cached values.'__dict__'to__slots__insideIPv6Addressfor the same reason, ensuring we preserve the memory efficiency benefits of slots for the remaining fixed attributes.