Skip to content

gh-115649: Fix double free for intern strings dict #120290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,6 @@ init_interned_dict(PyInterpreterState *interp)
static void
clear_interned_dict(PyInterpreterState *interp)
{
PyObject *interned = get_interned_dict(interp);
if (interned != NULL) {
PyDict_Clear(interned);
Py_DECREF(interned);
_Py_INTERP_CACHED_OBJECT(interp, interned_strings) = NULL;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand the crash report correctly, the problem is that the interned dict is cleared for sub-interpreters when it is also shared between the main interpreter and sub-interpreters. This change means that PyDict_Clear() is never called, not even for the main interpreter. That seems like it will just leak everything in the dict.

I imagine, rather, that you'd put this code inside the block below that is guarded by the _Py_IsMainInterpreter() checl.

Copy link
Contributor Author

@aisk aisk Jun 9, 2024

Choose a reason for hiding this comment

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

Thanks for the review! Sorry, I put this PR too earlier, after some research I found that I was wrong, and the crash is caused by other place.

For now I can ensure that this line

PyUnicode_InternInPlace(&filename);
caused the crash. It will intern the file name for the _tkinter.so, and after the interned dict finalization, call free on this object will crash.

I guess maybe this unicode object is created in another interpreter, and intern it in the main interpreter will cause the error. But I'm not familiar with these code, and still working on it.

if (_Py_IsMainInterpreter(interp) && INTERNED_STRINGS != NULL) {
_Py_hashtable_destroy(INTERNED_STRINGS);
INTERNED_STRINGS = NULL;
Expand Down Expand Up @@ -15533,9 +15527,6 @@ _PyUnicode_Fini(PyInterpreterState *interp)
{
struct _Py_unicode_state *state = &interp->unicode;

// _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
assert(get_interned_dict(interp) == NULL);

_PyUnicode_FiniEncodings(&state->fs_codec);

// bpo-47182: force a unicodedata CAPI capsule re-import on
Expand Down
Loading