bpo-46430: Fix memory leak in interned strings of deep-frozen modules (GH-31549)

This commit is contained in:
Kumar Aditya 2022-02-24 22:24:06 +05:30 committed by GitHub
parent 042f31da55
commit 4dc746310b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 6 deletions

View File

@ -65,7 +65,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
extern void _Py_Deepfreeze_Init(void);
/* Various internal finalizers */

View File

@ -0,0 +1 @@
Fix memory leak in interned strings of deep-frozen modules.

View File

@ -14,9 +14,13 @@
#include "Python/frozen_modules/importlib._bootstrap_external.h"
/* End includes */
/* Empty finalizer for deepfrozen modules*/
/* Empty initializer for deepfrozen modules */
void _Py_Deepfreeze_Init(void)
{
}
/* Empty finalizer for deepfrozen modules */
void
_Py_Deepfreeze_Fini(void)
_Py_Deepfreeze_Fini(void)
{
}

View File

@ -22,6 +22,10 @@
#include <unistd.h>
#endif
/* Empty initializer for deepfrozen modules */
void _Py_Deepfreeze_Init(void)
{
}
/* Empty finalizer for deepfrozen modules */
void
_Py_Deepfreeze_Fini(void)

View File

@ -754,7 +754,6 @@ pycore_init_types(PyInterpreterState *interp)
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_OK();
}
@ -827,7 +826,10 @@ pycore_interp_init(PyThreadState *tstate)
if (_PyStatus_EXCEPTION(status)) {
return status;
}
// Intern strings in deep-frozen modules first so that others
// can use it instead of creating a heap allocated string.
_Py_Deepfreeze_Init();
status = pycore_init_types(interp);
if (_PyStatus_EXCEPTION(status)) {
goto done;

View File

@ -110,6 +110,7 @@ class Printer:
self.hits, self.misses = 0, 0
self.patchups: list[str] = []
self.deallocs: list[str] = []
self.interns: list[str] = []
self.write('#include "Python.h"')
self.write('#include "internal/pycore_gc.h"')
self.write('#include "internal/pycore_code.h"')
@ -279,7 +280,7 @@ class Printer:
self.write(f".co_cellvars = {co_cellvars},")
self.write(f".co_freevars = {co_freevars},")
self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
self.patchups.append(f"_PyStaticCode_InternStrings(&{name});")
self.interns.append(f"_PyStaticCode_InternStrings(&{name});")
return f"& {name}.ob_base"
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
@ -446,6 +447,9 @@ def generate(args: list[str], output: TextIO) -> None:
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
for p in printer.deallocs:
printer.write(p)
with printer.block(f"void\n_Py_Deepfreeze_Init(void)"):
for p in printer.interns:
printer.write(p)
if verbose:
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")