Skip to content

Commit 87e7cb0

Browse files
gh-105699: Fix an Interned Strings Crasher (gh-106930)
A static (process-global) str object must only have its "interned" state cleared when no longer interned in any interpreters. They are the only ones that can be shared by interpreters so we don't have to worry about any other str objects. We trigger clearing the state with the main interpreter, since no other interpreters may exist at that point and _PyUnicode_ClearInterned() is only called during interpreter finalization. We do not address here the fact that a string will only be interned in the first interpreter that interns it. In any subsequent interpreters str.state.interned is already set so _PyUnicode_InternInPlace() will skip it. That needs to be addressed separately from fixing the crasher.
1 parent fd84ac0 commit 87e7cb0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Python no longer crashes due an infrequent race when initialzing
2+
per-interpreter interned strings. The crash would manifest when the
3+
interpreter was finalized.

Objects/unicodeobject.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -14818,6 +14818,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1481814818
PyObject *s, *ignored_value;
1481914819
while (PyDict_Next(interned, &pos, &s, &ignored_value)) {
1482014820
assert(PyUnicode_IS_READY(s));
14821+
int shared = 0;
1482114822
switch (PyUnicode_CHECK_INTERNED(s)) {
1482214823
case SSTATE_INTERNED_IMMORTAL:
1482314824
// Skip the Immortal Instance check and restore
@@ -14829,6 +14830,14 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1482914830
#endif
1483014831
break;
1483114832
case SSTATE_INTERNED_IMMORTAL_STATIC:
14833+
/* It is shared between interpreters, so we should unmark it
14834+
only when this is the last interpreter in which it's
14835+
interned. We immortalize all the statically initialized
14836+
strings during startup, so we can rely on the
14837+
main interpreter to be the last one. */
14838+
if (!_Py_IsMainInterpreter(interp)) {
14839+
shared = 1;
14840+
}
1483214841
break;
1483314842
case SSTATE_INTERNED_MORTAL:
1483414843
/* fall through */
@@ -14837,7 +14846,9 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1483714846
default:
1483814847
Py_UNREACHABLE();
1483914848
}
14840-
_PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
14849+
if (!shared) {
14850+
_PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
14851+
}
1484114852
}
1484214853
#ifdef INTERNED_STATS
1484314854
fprintf(stderr,

0 commit comments

Comments
 (0)