Skip to content
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

gh-105699: Fix an Interned Strings Crasher #106930

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Python no longer crashes due an infrequent race when initialzing
per-interpreter interned strings. The crash would manifest when the
interpreter was finalized.
13 changes: 12 additions & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -14818,6 +14818,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
PyObject *s, *ignored_value;
while (PyDict_Next(interned, &pos, &s, &ignored_value)) {
assert(PyUnicode_IS_READY(s));
int shared = 0;
switch (PyUnicode_CHECK_INTERNED(s)) {
case SSTATE_INTERNED_IMMORTAL:
// Skip the Immortal Instance check and restore
Expand All @@ -14829,6 +14830,14 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
#endif
break;
case SSTATE_INTERNED_IMMORTAL_STATIC:
/* It is shared between interpreters, so we should unmark it
only when this is the last interpreter in which it's
interned. We immortalize all the statically initialized
strings during startup, so we can rely on the
main interpreter to be the last one. */
if (!_Py_IsMainInterpreter(interp)) {
shared = 1;
}
break;
case SSTATE_INTERNED_MORTAL:
/* fall through */
Expand All @@ -14837,7 +14846,9 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
default:
Py_UNREACHABLE();
}
_PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
if (!shared) {
_PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
}
}
#ifdef INTERNED_STATS
fprintf(stderr,
Expand Down