[Python-checkins] bpo-36854: Fix refleak in subinterpreter (GH-17331)

Victor Stinner webhook-mailer at python.org
Fri Nov 22 04:58:04 EST 2019


https://github.com/python/cpython/commit/310e2d25170a88ef03f6fd31efcc899fe062da2c
commit: 310e2d25170a88ef03f6fd31efcc899fe062da2c
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2019-11-22T10:58:00+01:00
summary:

bpo-36854: Fix refleak in subinterpreter (GH-17331)

finalize_interp_clear() now explicitly clears the codec registry and
then trigger a GC collection to clear all references.

files:
M Modules/_testcapimodule.c
M Python/pylifecycle.c

diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index baa6907b7e4f6..0908f3457f580 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -6721,11 +6721,14 @@ PyInit__testcapi(void)
     PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
 
     PyModule_AddIntConstant(m, "the_number_three", 3);
+    PyObject *v;
 #ifdef WITH_PYMALLOC
-    PyModule_AddObject(m, "WITH_PYMALLOC", Py_True);
+    v = Py_True;
 #else
-    PyModule_AddObject(m, "WITH_PYMALLOC", Py_False);
+    v = Py_False;
 #endif
+    Py_INCREF(v);
+    PyModule_AddObject(m, "WITH_PYMALLOC", v);
 
     TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
     Py_INCREF(TestError);
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 7591f069b455d..8c508e33800ef 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1210,6 +1210,14 @@ finalize_interp_clear(PyThreadState *tstate)
 {
     int is_main_interp = _Py_IsMainInterpreter(tstate);
 
+    /* bpo-36854: Explicitly clear the codec registry
+       and trigger a GC collection */
+    PyInterpreterState *interp = tstate->interp;
+    Py_CLEAR(interp->codec_search_path);
+    Py_CLEAR(interp->codec_search_cache);
+    Py_CLEAR(interp->codec_error_registry);
+    _PyGC_CollectNoFail();
+
     /* Clear interpreter state and all thread states */
     PyInterpreterState_Clear(tstate->interp);
 



More information about the Python-checkins mailing list