You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before commit 8334f30, at the first _imp.create_builtin() calls, it calls _PyImport_FixupExtensionObject() which stores the created module in an internal "extensions" dictionary. PyInit__weakref() returns a module object. Next calls to _imp.create_builtin("_weakref") simply returned the cached _weakref module.
At commit 8334f30, _imp.create_builtin() doesn't store it in the internal "extensions" dictionary, but calls PyModule_FromDefAndSpec() instead. PyInit__weakref() returns a module definition (PyModuleDef_Type: "moduledef"). Each _imp.create_builtin("_weakref") creates a new module.
Raise ImportError if the module can't be imported.
"""
# try to import the module and raise an error if it can't be imported
if name not in sys.modules:
__import__(name)
del sys.modules[name]
for modname in list(sys.modules):
if modname == name or modname.startswith(name + '.'):
orig_modules[modname] = sys.modules[modname]
del sys.modules[modname]
finally:
for orig_name, module in orig_modules.items():
sys.modules[orig_name] = module
for name_to_remove in names_to_remove:
del sys.modules[name_to_remove]
I still get a leak:
---
test_leak leaked [34, 34, 34] references, sum=102
test_leak leaked [11, 11, 11] memory blocks, sum=33
---
I understand that sys.modules["_weakref"] is overriden by the test with a new module, and the new module holds a reference somehow to the previous _weakref module. The old and the new modules remain alive.
machinery = test.test_importlib.util.import_importlib('importlib.machinery') is called: it reloads the whole importlib package, with _frozen_importlib and _frozen_importlib_external blocked (force to reload pure Python importlib._bootstrap and importlib._bootstrap_external modules)
support.import_module('winreg', required_on=['win']) is called with the fresh new importlib package: in short, it tries to import winreg which fails with ModuleNotFoundError
importlib._bootstrap._setup() is called: it copies '_thread', '_warnings' and '_weakref' from sys.modules into importlib._bootstrap namespace (module globals) using setattr(self_module, ...)
importlib._bootstrap_external._setup() is called: it calls _bootstrap._builtin_from_name('_weakref')
When importlib._bootstrap_external._setup() calls _bootstrap._builtin_from_name('_weakref'), _init_module_attrs() copies the "spec" into module.__spec__. But the spec contains a reference to importlib._bootstrap namespace.
Before _weakref is converted to multiphase initialization:
Before _weakref is converted to multiphase initialization:
(...)
=> same module
That's because modules which don't use the multiphase initialization are cached in _PyImport_FixupExtensionObject(): see msg364914 for details. Next calls to _imp.create_builtin("_weakref") simply returned the cached _weakref module. It's loaded exactly once.
Maybe the test_importlib should before save/restore the the "Python state" rather than modifying modules
You will have to be more specific than that as there is an import_state() context manager to control import state via the sys module.
Maybe module.__spec__ should leak less importlib internals: explicitly clear namespaces? Use static methods? I'm not sure how to do that.
Are you saying change everything on __spec__ objects to be static methods? That won't work because the whole point of __spec__ objects is to essentially be data classes to store details of a module.
Remove module.__spec__? ... that would mean rejecting PEP-451 implemented in Python 3.4, that sounds like a major regression :-(
No. You would break the world and undo years of work to clean up import semantics with no good way to go back to the old way.
Can I ask why you suddenly want to throw __spec__ objects out due to a leak tied back to _weakref switching to multi-phase initialization? Also note that the use of weakrefs by importlib isn't tied to __spec__ objects but to per-module import locks in importlib._bootstrap.
I pushed commit 83d46e0 which should not be controversial. In short, the fix is to remove two unused imports :-D
The fix doesn't remove module.__spec__ nor avoid usage of _weakref, it only fix this issue (reference leak) by copying code from _bootstrap.py to _bootstrap_external.py. In fact, modules like _io were already correctly imported by _bootstrap_external._setup(). Only _thread, _weakref and winreg imports caused this bug.
I don't think that it's worth it to backport the change to stable branches 3.7 and 3.8, since stable branches don't use multiphase initialization for _weakref. The two unused imports don't cause any harm in importlib._bootstrap_external.
Can I ask why you suddenly want to throw __spec__ objects out due to a leak tied back to _weakref switching to multi-phase initialization?
I was thinking just aloud to try to find a solution for this reference leak.
Since this reference leak is fixed, I reapplied Hai Shi's change for _weakref in bpo-1635741:
New changeset 93460d0 by Victor Stinner in branch 'master': bpo-1635741: Port _weakref extension module to multiphase initialization (PEP-489) (GH-19140) 93460d0
Activity
vstinner commentedon Mar 23, 2020
https://buildbot.python.org/all/#/builders/206/builds/119
test_importlib leaked [6303, 6299, 6303] references, sum=18905
test_importlib leaked [2022, 2020, 2022] memory blocks, sum=6064
Issue reported at: https://bugs.python.org/issue1635741#msg364845
It seems like the regression was introduced by the following change:
commit 8334f30 (HEAD)
Author: Hai Shi <shihai1992@gmail.com>
Date: Fri Mar 20 16:16:45 2020 +0800
vstinner commentedon Mar 23, 2020
New changeset 188078c by Victor Stinner in branch 'master':
Revert "bpo-1635741: Port _weakref extension module to multiphase initialization (PEP-489) (GH-19084)" (bpo-19128)
188078c
vstinner commentedon Mar 24, 2020
I can reproduce the leak with the following test, stored as Lib/test/test_leak.py:
---
Extract of "./python -m test -R 3:3 test_leak" output:
---
test_leak leaked [6303, 6299, 6303] references, sum=18905
test_leak leaked [2022, 2020, 2022] memory blocks, sum=6064
---
vstinner commentedon Mar 24, 2020
Before commit 8334f30, at the first _imp.create_builtin() calls, it calls _PyImport_FixupExtensionObject() which stores the created module in an internal "extensions" dictionary. PyInit__weakref() returns a module object. Next calls to _imp.create_builtin("_weakref") simply returned the cached _weakref module.
At commit 8334f30, _imp.create_builtin() doesn't store it in the internal "extensions" dictionary, but calls PyModule_FromDefAndSpec() instead. PyInit__weakref() returns a module definition (PyModuleDef_Type: "moduledef"). Each _imp.create_builtin("_weakref") creates a new module.
vstinner commentedon Mar 24, 2020
I can reproduce the leak with command:
./python -m test -R 3:3 test_leak
File 1:
---
File 2: Lib/importlib3.py
---
try:
import _frozen_importlib as _bootstrap
case = 1
except ImportError:
case = 2
---
vstinner commentedon Mar 24, 2020
With the latest Lib/importlib3.py, there are less leaked references, but there are still leaked references:
test_leak leaked [139, 139, 139] references, sum=417
test_leak leaked [42, 42, 42] memory blocks, sum=126
vstinner commentedon Mar 24, 2020
Oops, you should read:
File 1: Lib/test/test_leak.py
vstinner commentedon Mar 24, 2020
Code extremely simplified:
---
I still get a leak:
---
test_leak leaked [34, 34, 34] references, sum=102
test_leak leaked [11, 11, 11] memory blocks, sum=33
---
I understand that sys.modules["_weakref"] is overriden by the test with a new module, and the new module holds a reference somehow to the previous _weakref module. The old and the new modules remain alive.
vstinner commentedon Mar 24, 2020
Relationship between origin unmodified code and the extremely simplified code:
Lib/test/test_importlib/init.py: load_tests() is called at each "test -R 3:3" iteration
Lib/test/test_importlib/test_windows.py: at each iteartion:
Lib/importlib/init.py: each time the module is reloaded
When importlib._bootstrap_external._setup() calls _bootstrap._builtin_from_name('_weakref'), _init_module_attrs() copies the "spec" into module.__spec__. But the spec contains a reference to importlib._bootstrap namespace.
Before _weakref is converted to multiphase initialization:
=> same module
After the change:
=> two different objects
The problem is that module.__spec__ pulls the whole importlib package which contains tons of objects.
vstinner commentedon Mar 24, 2020
Quick & dirty workaround:
But I think that the issue is larger than just _weakref.
[-]test_importlib leaked [6303, 6299, 6303] references[/-][+]importlib: module.__spec__ leaks importlib namespaces (test_importlib leaked xxx references)[/+][-]test_importlib leaked [6303, 6299, 6303] references[/-][+]importlib: module.__spec__ leaks importlib namespaces (test_importlib leaked xxx references)[/+]vstinner commentedon Mar 24, 2020
That's because modules which don't use the multiphase initialization are cached in _PyImport_FixupExtensionObject(): see msg364914 for details. Next calls to _imp.create_builtin("_weakref") simply returned the cached _weakref module. It's loaded exactly once.
vstinner commentedon Mar 24, 2020
New changeset 83d46e0 by Victor Stinner in branch 'master':
bpo-40050: Fix importlib._bootstrap_external (GH-19135)
83d46e0
brettcannon commentedon Mar 24, 2020
From Victor:
You will have to be more specific than that as there is an import_state() context manager to control import state via the sys module.
Are you saying change everything on __spec__ objects to be static methods? That won't work because the whole point of __spec__ objects is to essentially be data classes to store details of a module.
No. You would break the world and undo years of work to clean up import semantics with no good way to go back to the old way.
Can I ask why you suddenly want to throw __spec__ objects out due to a leak tied back to _weakref switching to multi-phase initialization? Also note that the use of weakrefs by importlib isn't tied to __spec__ objects but to per-module import locks in importlib._bootstrap.
vstinner commentedon Mar 24, 2020
I close the issue.
I pushed commit 83d46e0 which should not be controversial. In short, the fix is to remove two unused imports :-D
The fix doesn't remove module.__spec__ nor avoid usage of _weakref, it only fix this issue (reference leak) by copying code from _bootstrap.py to _bootstrap_external.py. In fact, modules like _io were already correctly imported by _bootstrap_external._setup(). Only _thread, _weakref and winreg imports caused this bug.
I don't think that it's worth it to backport the change to stable branches 3.7 and 3.8, since stable branches don't use multiphase initialization for _weakref. The two unused imports don't cause any harm in importlib._bootstrap_external.
I was thinking just aloud to try to find a solution for this reference leak.
vstinner commentedon Mar 24, 2020
Since this reference leak is fixed, I reapplied Hai Shi's change for _weakref in bpo-1635741:
New changeset 93460d0 by Victor Stinner in branch 'master':
bpo-1635741: Port _weakref extension module to multiphase initialization (PEP-489) (GH-19140)
93460d0
vstinner commentedon Mar 25, 2020
New changeset ace018c by Victor Stinner in branch 'master':
bpo-40050: Rephrase NEWS entry (GH-19148)
ace018c