Skip to content

gh-70368: Fix multiple registration of ForkAwareLocal atfork cleaner - #13986

Closed
orivej wants to merge 3 commits into
python:mainfrom
orivej:fix-bpo-26180
Closed

orivej wants to merge 3 commits into
python:mainfrom
orivej:fix-bpo-26180

Conversation

@orivej

@orivej orivej commented Jun 11, 2019 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

A test for the change would be awesome

@orivej

orivej commented Jun 11, 2019

Copy link
Copy Markdown
Contributor Author

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

@orivej

orivej commented Jun 11, 2019

Copy link
Copy Markdown
Contributor Author

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

@jdemeyer jdemeyer left a comment

Copy link
Copy Markdown
Contributor

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

orivej commented Jun 14, 2019

Copy link
Copy Markdown
Contributor Author

__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
Copy Markdown
Contributor

OK, you convinced me.

@orivej

orivej commented Jul 2, 2019

Copy link
Copy Markdown
Contributor Author

What shall happen next?

@jdemeyer

jdemeyer commented Jul 2, 2019

Copy link
Copy Markdown
Contributor

We need a core developer to review your PR.

@orivej

orivej commented Aug 3, 2019

Copy link
Copy Markdown
Contributor Author

Monthly ping.

@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 7, 2026
@serhiy-storchaka serhiy-storchaka changed the title bpo-26180: Fix multiple registration of ForkAwareLocal atfork cleaner gh-70368: Fix multiple registration of ForkAwareLocal atfork cleaner Aug 12, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 14, 2026
@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR has not been touched by a human in more than 5 years. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer on the related issue.

@ambv ambv closed this Oct 20, 2026
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.

7 participants