Skip to content

Commit 957f14d

Browse files
[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 0ba07b2) Co-authored-by: Eric Snow <[email protected]>
1 parent ffc7678 commit 957f14d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed
Lines changed: 4 additions & 0 deletions
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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15177,10 +15177,13 @@ init_fs_codec(PyInterpreterState *interp)
1517715177

1517815178
/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
1517915179
global configuration variables. */
15180-
if (_Py_SetFileSystemEncoding(fs_codec->encoding,
15181-
fs_codec->errors) < 0) {
15182-
PyErr_NoMemory();
15183-
return -1;
15180+
if (_Py_IsMainInterpreter(interp)) {
15181+
15182+
if (_Py_SetFileSystemEncoding(fs_codec->encoding,
15183+
fs_codec->errors) < 0) {
15184+
PyErr_NoMemory();
15185+
return -1;
15186+
}
1518415187
}
1518515188
return 0;
1518615189
}

0 commit comments

Comments
 (0)