Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
(cherry picked from commit 0ba07b2)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
  • Loading branch information
ericsnowcurrently authored and miss-islington committed Jul 21, 2023
commit 7264c6d38fc5c16bd8ba41318b3842f9be1c1928
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Python no longer crashes due to an infrequent race in setting
``Py_FileSystemDefaultEncoding`` and ``Py_FileSystemDefaultEncodeErrors``
(both deprecated), when simultaneously initializing two isolated
subinterpreters. Now they are only set during runtime initialization.
11 changes: 7 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15177,10 +15177,13 @@ init_fs_codec(PyInterpreterState *interp)

/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
global configuration variables. */
if (_Py_SetFileSystemEncoding(fs_codec->encoding,
fs_codec->errors) < 0) {
PyErr_NoMemory();
return -1;
if (_Py_IsMainInterpreter(interp)) {

if (_Py_SetFileSystemEncoding(fs_codec->encoding,
fs_codec->errors) < 0) {
PyErr_NoMemory();
return -1;
}
}
return 0;
}
Expand Down