[3.12] gh-105699: Fix a Crasher Related to a Deprecated Global Variable (gh-106923) (#106964)

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 0ba07b2108)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-07-21 13:27:52 -07:00 committed by GitHub
parent ffc7678f46
commit 957f14d0de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View file

@ -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;
}