Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-1635741: Fix refleaks of encodings module by removing the encodings._aliases#21896
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
bpo-1635741: Fix refleaks of encodings module by removing the encodings._aliases #21896
Uh oh!
There was an error while loading. Please reload this page.
Conversation
shihai1991 commented Aug 16, 2020 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
shihai1991 commented Aug 16, 2020
I use the test case of https://bugs.python.org/issue1635741#msg355187 to test the refleaks in debug mode. Before this PR: After this PR: |
shihai1991 commented Aug 16, 2020
@vstinner Hi, victor. Pls take a look if you have free time, thanks. |
Lib/encodings/__init__.py Outdated
| defsearch_function(encoding): | ||
| _aliases=aliases.aliases |
ghostAug 16, 2020 • edited by ghost
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by ghost
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.
Don't understand the problem.
This statement should be placed at below so that it does not affect the performance of the cache.
shihai1991Aug 16, 2020 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
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.
Don't understand the problem.
Thanks for your comment. It will affect the encodings module's refcount in C level and reduce the refleaks.
This statement should be placed at below so that it does not affect the performance of the cache.
MAYBE removing this line and using aliases.aliases to replace _aliases is fine too :)
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.
Looking this comment: #21896 (comment)
ghostAug 16, 2020 • edited by ghost
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by ghost
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.
Looking this comment: #21896 (comment)
The usage of aliases.aliasesis is very normal, maybe the root of the problem is not here.
vstinner left a comment
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 don't see how using encodings._aliases in search_function() creates a "reference leak". A leak is when calling a function multiple times leaks memory. Here, there is no leak.
Maybe you're talking about a "reference cycle".
I guess that you're trying to clear variables at exit.
You should try to trigger an explicit GC collection after calling PyInterpreterState_Clear(). In finalize_interp_clear(), try to replace:
/* Trigger a GC collection on subinterpreters*/ if (!is_main_interp){_PyGC_CollectNoFail()} with:
// Last explicit GC collection _PyGC_CollectNoFail(); (without this change)
Does it fix your issue?
PyInterpreterState_Clear() clears the reference to the search function: Py_CLEAR(interp->codec_search_path).
shihai1991 commented Aug 17, 2020 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Thanks, victor. "reference cycle" would be more exact. And I will try your idea in my interpreter. |
shihai1991 commented Aug 17, 2020
Oh, amazing result: sys.gettotalrefcount: 10537 the pr in: #21902 |
shihai1991 commented Aug 17, 2020 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Pablo created this PR(don't calling explict collection in main interpreter): #17457 |
vstinner commented Aug 17, 2020
Since #17457 is merged, is this PR still relevant/useless? If not, please close it. |
Fix refleaks of
encodings._aliasesby usingencodings.aliasesdirectly inencodings.search_function.Co-authored-by: Victor Stinner vstinner@python.org
https://bugs.python.org/issue1635741