Skip to content

Commit 7e6ef78

Browse files
committed
Clear func_version_cache in interpreter_clear()
This should fix leaks and hopefully most failing tests.
1 parent ab74ef0 commit 7e6ef78

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

Include/internal/pycore_function.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr)
3030
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
3131
extern void _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
3232
PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version);
33-
void _PyFunction_ClearByVersionCache(void);
33+
void _PyFunction_ClearByVersionCache(PyInterpreterState *interp);
3434

3535
extern PyObject *_Py_set_function_type_params(
3636
PyThreadState* unused, PyObject *func, PyObject *type_params);

Objects/funcobject.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,8 @@ _PyFunction_LookupByVersion(uint32_t version)
250250
}
251251

252252
void
253-
_PyFunction_ClearByVersionCache(void)
253+
_PyFunction_ClearByVersionCache(PyInterpreterState *interp)
254254
{
255-
PyInterpreterState *interp = _PyInterpreterState_GET();
256255
for (int i = 0; i < FUNC_VERSION_CACHE_SIZE; i++) {
257256
PyFunctionObject **slot = interp->func_state.func_version_cache + i;
258257
Py_CLEAR(*slot);

Python/pystate.c

+3
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
840840
*/
841841
// XXX Make sure we properly deal with problematic finalizers.
842842

843+
interp->func_state.next_version = 0; // No more new versions
844+
_PyFunction_ClearByVersionCache(interp);
845+
843846
Py_CLEAR(interp->audit_hooks);
844847

845848
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {

Python/sysmodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,8 @@ sys__clear_type_cache_impl(PyObject *module)
20802080
{
20812081
PyType_ClearCache();
20822082
// Also clear the function-by-version cache
2083-
_PyFunction_ClearByVersionCache();
2083+
PyInterpreterState *interp = _PyInterpreterState_GET();
2084+
_PyFunction_ClearByVersionCache(interp);
20842085
Py_RETURN_NONE;
20852086
}
20862087

0 commit comments

Comments
 (0)