This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: multiprocessing.util._afterfork_registry leak in threaded environment
Type: resource usage Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: davin, jdemeyer, mzamazal, nirs, terry.reedy
Priority: normal Keywords: patch

Created on 2016-01-22 15:24 by mzamazal, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
bug.py mzamazal, 2016-01-22 15:24
bug3.py mzamazal, 2016-01-25 08:22
Pull Requests
URL Status Linked Edit
PR 13986 open orivej, 2019-06-11 20:32
Messages (6)
msg258816 - (view) Author: Milan Zamazal (mzamazal) Date: 2016-01-22 15:24
When calling multiprocessing.managers.BaseManager repeatedly from a client, each time from a new thread, new entry with (already existent) multiprocessing.util.ForkAwareLocal instance is inserted as a value into multiprocessing.util._afterfork_registry dictionary on each of the calls.  So the dictionary grows on each client call and may grow so indefinitely, causing memory leak if nothing else.

The attached file demonstrates the problem (Python 2.7 version, it's reproducible on 3.4 as well after 2->3 adjustments).  Just run it and look at the output.  The printed dictionary contains 10 entries holding the same ForkAwareLocal instance although there should be probably just one such entry there.
msg258850 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-01-23 00:27
Please post your 3.x version.  3.4 only gets security patches now.
msg258896 - (view) Author: Milan Zamazal (mzamazal) Date: 2016-01-25 08:22
Python 3.x version attached.
msg345473 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2019-06-13 08:20
Why is it that __init__ is called multiple times? I'm just wondering if we could somehow ensure that __init__ is called only once.
msg345524 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-06-13 15:47
With master and Windows 10, the output looks like
(0, 61333912, <function ForkAwareThreadLock._reset at 0x03A783F0>): <multiprocessing.util.ForkAwareThreadLock object at 0x03A7E198>
(1, 63524512, <function ForkAwareLocal.__init__.<locals>.<lambda> at 0x03CA2940>): <multiprocessing.util.ForkAwareLocal object at 0x03C94EA0>
(2, 60787504, <function ProcessLocalSet.__init__.<locals>.<lambda> at 0x03CA2760>): ProcessLocalSet()
(5, 63524512, <function ForkAwareLocal.__init__.<locals>.<lambda> at 0x03CA2C60>): <multiprocessing.util.ForkAwareLocal object at 0x03C94EA0>
... <8 more lines like the two with ForkAwareLocal.__init__....>

I am not familiar with multiprocessing Managers and the doc is not completely clear to me.  Does bug3.py only run in one process or does it result in multiple processes?  If the latter, I expect it would need an "if __name__ == '__main__':" clause, at least on Windows.  (Milan, I should have asked what OS you used, as mp has OS-specific behaviors.)
(https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods, Safe importing of main module)
msg345561 - (view) Author: Milan Zamazal (mzamazal) Date: 2019-06-14 07:05
I used Linux.
History
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70368
2019-06-14 07:05:58mzamazalsetmessages: + msg345561
2019-06-13 15:47:23terry.reedysetmessages: + msg345524
versions: + Python 3.9, - Python 2.7, Python 3.5, Python 3.6
2019-06-13 08:20:11jdemeyersetnosy: + jdemeyer
messages: + msg345473
2019-06-11 20:32:08orivejsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13849
2016-02-12 03:58:29ned.deilysetnosy: + davin
2016-01-29 16:01:15nirssetnosy: + nirs
2016-01-25 08:22:10mzamazalsetfiles: + bug3.py

messages: + msg258896
2016-01-23 00:27:10terry.reedysetnosy: + terry.reedy

messages: + msg258850
versions: + Python 3.5, Python 3.6, - Python 3.4
2016-01-22 15:24:46mzamazalcreate