Skip to content

Commit 0ba07b2

Browse files
gh-105699: Fix a Crasher Related to a Deprecated Global Variable (gh-106923)
There was a slight race in _Py_ClearFileSystemEncoding() (when called from _Py_SetFileSystemEncoding()), between freeing the value and setting the variable to NULL, which occasionally caused crashes when multiple isolated interpreters were used. (Notably, I saw at least 10 different, seemingly unrelated spooky-action-at-a-distance, ways this crashed. Yay, free threading!) We avoid the problem by only setting the global variables with the main interpreter (i.e. runtime init).
1 parent 87e7cb0 commit 0ba07b2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Python no longer crashes due to an infrequent race in setting
2+
``Py_FileSystemDefaultEncoding`` and ``Py_FileSystemDefaultEncodeErrors``
3+
(both deprecated), when simultaneously initializing two isolated
4+
subinterpreters. Now they are only set during runtime initialization.

Objects/unicodeobject.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -15189,10 +15189,13 @@ init_fs_codec(PyInterpreterState *interp)
1518915189

1519015190
/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
1519115191
global configuration variables. */
15192-
if (_Py_SetFileSystemEncoding(fs_codec->encoding,
15193-
fs_codec->errors) < 0) {
15194-
PyErr_NoMemory();
15195-
return -1;
15192+
if (_Py_IsMainInterpreter(interp)) {
15193+
15194+
if (_Py_SetFileSystemEncoding(fs_codec->encoding,
15195+
fs_codec->errors) < 0) {
15196+
PyErr_NoMemory();
15197+
return -1;
15198+
}
1519615199
}
1519715200
return 0;
1519815201
}

0 commit comments

Comments
 (0)