aboutsummaryrefslogtreecommitdiff
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-03 18:07:15 +0100
committerGitHub <noreply@github.com>2020-11-03 18:07:15 +0100
commitfd957c124c44441d9c5eaf61f7af8cf266bafcb1 (patch)
tree2d73071bb42fe4496d11d79007eb4930fe5fd673 /Python/pystate.c
parent212d32f45c91849c17a82750df1ac498d63976be (diff)
downloadcpython3-fd957c124c44441d9c5eaf61f7af8cf266bafcb1.tar.gz
bpo-41796: Call _PyAST_Fini() earlier to fix a leak (GH-23131)
Call _PyAST_Fini() on all interpreters, not only on the main interpreter. Also, call it ealier to fix a reference leak. Python types contain a reference to themselves in in their PyTypeObject.tp_mro member. _PyAST_Fini() must called before the last GC collection to destroy AST types. _PyInterpreterState_Clear() now calls _PyAST_Fini(). It now also calls _PyWarnings_Fini() on subinterpeters, not only on the main interpreter. Add an assertion in AST init_types() to ensure that the _ast module is no longer used after _PyAST_Fini() has been called.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index e37cbd5a65..c9882a7f74 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -300,13 +300,16 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
Py_CLEAR(interp->after_forkers_parent);
Py_CLEAR(interp->after_forkers_child);
#endif
- if (_PyRuntimeState_GetFinalizing(runtime) == NULL) {
- _PyWarnings_Fini(interp);
- }
+
+ _PyAST_Fini(interp);
+ _PyWarnings_Fini(interp);
+
+ // All Python types must be destroyed before the last GC collection. Python
+ // types create a reference cycle to themselves in their in their
+ // PyTypeObject.tp_mro member (the tuple contains the type).
/* Last garbage collection on this interpreter */
_PyGC_CollectNoFail(tstate);
-
_PyGC_Fini(tstate);
/* We don't clear sysdict and builtins until the end of this function.