8000 bpo-26180: Fix multiple registration of ForkAwareLocal atfork cleaner by orivej · Pull Request #13986 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-26180: Fix multiple registration of ForkAwareLocal atfork cleaner #13986

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

orivej
Copy link
Contributor
8000 orivej commented Jun 11, 2019

threading.local instance __init__ method is called multiple times, once per
thread where the instance is used, but ForkAwareLocal atfork handler needs to be
registered just once when its instance is created. If it is registered many
times, the registrations bloat _afterfork_registry as long as the instance is
alive, and can exhaust all memory.

https://bugs.python.org/issue26180

`threading.local` instance `__init__` method is called multiple times, once per
thread where the instance is used, but ForkAwareLocal atfork handler needs to be
registered just once when its instance is created. If it is registered many
times, the registrations bloat `_afterfork_registry` as long as the instance is
alive, and can exhaust all memory.
@asvetlov
Copy link
Contributor

A test for the change would be awesome

@orivej
Copy link
Contributor Author
orivej 8000 commented Jun 11, 2019

I have added a test TestForkAwareLocal modeled (and placed) after TestForkAwareThreadLock.

@orivej
Copy link
Contributor Author
orivej commented Jun 11, 2019

Added a NEWS blurb. Feel free to edit or squash commits as you see fit.

Copy link
Contributor
@jdemeyer jdemeyer left a comment

Choose a reason for hiding this comment

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

The change and test look good to me. The only thing that I'm not sure about is whether this is the best fix for the problem: as I mentioned on bpo-26810 I wonder if we could ensure that __init__ is not called multiple times.

@orivej
Copy link
Contributor Author
orivej commented Jun 14, 2019

__init__ is called multiple times by design of threading.local: https://github.com/python/cpython/blob/v3.7.3/Lib/_threading_local.py#L65-L67 . Since https://docs.python.org/3.7/library/threading.html#threading.local refers to this docstring, I guess this is a documented behaviour and changing it would be breaking.

@jdemeyer
Copy link
Contributor

OK, you convinced me.

@orivej
Copy link
Contributor Author
orivej commented Jul 2, 2019

What shall happen next?

@jdemeyer
Copy link
Contributor
jdemeyer commented Jul 2, 2019

We need a core developer to review your PR.

@orivej
Copy link
Contributor Author
orivej commented Aug 3, 2019

Monthly ping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0