bpo-1635741: Fix refleaks of encodings module by removing the encodings._aliases by shihai1991 · Pull Request #21896 · python/cpython

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how using encodings._aliases in search_function() creates a "reference leak". A leak is when calling a function multiple times leaks memory. Here, there is no leak.

Maybe you're talking about a "reference cycle".

I guess that you're trying to clear variables at exit.

You should try to trigger an explicit GC collection after calling PyInterpreterState_Clear(). In finalize_interp_clear(), try to replace:

    /* Trigger a GC collection on subinterpreters*/
    if (!is_main_interp) {
        _PyGC_CollectNoFail();
    }

with:

    // Last explicit GC collection
    _PyGC_CollectNoFail();

(without this change)

Does it fix your issue?

PyInterpreterState_Clear() clears the reference to the search function: Py_CLEAR(interp->codec_search_path).