Skip to content

Commit 310e2d2

Browse files
authored
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.
1 parent 91daa9d commit 310e2d2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

‎Modules/_testcapimodule.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6721,11 +6721,14 @@ PyInit__testcapi(void)
67216721
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
67226722

67236723
PyModule_AddIntConstant(m, "the_number_three", 3);
6724+
PyObject *v;
67246725
#ifdef WITH_PYMALLOC
6725-
PyModule_AddObject(m, "WITH_PYMALLOC", Py_True);
6726+
v = Py_True;
67266727
#else
6727-
PyModule_AddObject(m, "WITH_PYMALLOC", Py_False);
6728+
v = Py_False;
67286729
#endif
6730+
Py_INCREF(v);
6731+
PyModule_AddObject(m, "WITH_PYMALLOC", v);
67296732

67306733
TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
67316734
Py_INCREF(TestError);

‎Python/pylifecycle.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,14 @@ finalize_interp_clear(PyThreadState *tstate)
12101210
{
12111211
int is_main_interp = _Py_IsMainInterpreter(tstate);
12121212

1213+
/* bpo-36854: Explicitly clear the codec registry
1214+
and trigger a GC collection */
1215+
PyInterpreterState *interp = tstate->interp;
1216+
Py_CLEAR(interp->codec_search_path);
1217+
Py_CLEAR(interp->codec_search_cache);
1218+
Py_CLEAR(interp->codec_error_registry);
1219+
_PyGC_CollectNoFail();
1220+
12131221
/* Clear interpreter state and all thread states */
12141222
PyInterpreterState_Clear(tstate->interp);
12151223

0 commit comments

Comments
 (0)