-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-151218: Fix data race in sys_set_flag for free-threading #151220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix a data race in :func:`sys.set_int_max_str_digits` when updating | ||
| :data:`sys.flags` in the free-threaded build. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,9 @@ Data members: | |
| #include "pycore_import.h" // _PyImport_SetDLOpenFlags() | ||
| #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() | ||
| #include "pycore_interpframe.h" // _PyFrame_GetFirstComplete() | ||
| #ifdef Py_GIL_DISABLED | ||
| # include "pycore_lock.h" // PyMutex_Lock() | ||
| #endif | ||
| #include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD | ||
| #include "pycore_modsupport.h" // _PyModule_CreateInitialized() | ||
| #include "pycore_namespace.h" // _PyNamespace_New() | ||
|
|
@@ -3476,13 +3479,31 @@ static PyStructSequence_Desc flags_desc = { | |
| // https://github.com/python/cpython/issues/122575#issuecomment-2416497086 | ||
| }; | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| static PyMutex sys_flags_mutex; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment to explain the purpose of this lock. IMO it should only be used when modifying sys.flags. It's not needed to acquire this lock to get the |
||
| #endif | ||
|
|
||
| static void | ||
| sys_set_flag(PyObject *flags, Py_ssize_t pos, PyObject *value) | ||
| sys_set_flag_unlocked(PyObject *flags, Py_ssize_t pos, PyObject *value, | ||
| PyObject **p_old_value) | ||
| { | ||
| assert(pos >= 0 && pos < (Py_ssize_t)(Py_ARRAY_LENGTH(flags_fields) - 1)); | ||
|
|
||
| PyObject *old_value = PyStructSequence_GET_ITEM(flags, pos); | ||
| *p_old_value = PyStructSequence_GET_ITEM(flags, pos); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you call |
||
| PyStructSequence_SET_ITEM(flags, pos, Py_NewRef(value)); | ||
| } | ||
|
|
||
| static void | ||
| sys_set_flag(PyObject *flags, Py_ssize_t pos, PyObject *value) | ||
| { | ||
| PyObject *old_value; | ||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Lock(&sys_flags_mutex); | ||
| #endif | ||
| sys_set_flag_unlocked(flags, pos, value, &old_value); | ||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Unlock(&sys_flags_mutex); | ||
| #endif | ||
| Py_XDECREF(old_value); | ||
| } | ||
|
|
||
|
|
@@ -3501,20 +3522,6 @@ _PySys_SetFlagObj(Py_ssize_t pos, PyObject *value) | |
| } | ||
|
|
||
|
|
||
| static int | ||
| _PySys_SetFlagInt(Py_ssize_t pos, int value) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can keep this function, it doesn't hurt. |
||
| { | ||
| PyObject *obj = PyLong_FromLong(value); | ||
| if (obj == NULL) { | ||
| return -1; | ||
| } | ||
|
|
||
| int res = _PySys_SetFlagObj(pos, obj); | ||
| Py_DECREF(obj); | ||
| return res; | ||
| } | ||
|
|
||
|
|
||
| static int | ||
| set_flags_from_config(PyInterpreterState *interp, PyObject *flags) | ||
| { | ||
|
|
@@ -4666,16 +4673,40 @@ _PySys_SetIntMaxStrDigits(int maxdigits) | |
| return -1; | ||
| } | ||
|
|
||
| // Set sys.flags.int_max_str_digits | ||
| const Py_ssize_t pos = SYS_FLAGS_INT_MAX_STR_DIGITS; | ||
| if (_PySys_SetFlagInt(pos, maxdigits) < 0) { | ||
| PyObject *obj = PyLong_FromLong(maxdigits); | ||
| if (obj == NULL) { | ||
| return -1; | ||
| } | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Lock(&sys_flags_mutex); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that you need to acquire the lock to get sys.flags. You only need the lock around the code changing sys.flags members. |
||
| #endif | ||
|
|
||
| PyObject *flags = PySys_GetAttrString("flags"); | ||
| if (flags == NULL) { | ||
| Py_DECREF(obj); | ||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Unlock(&sys_flags_mutex); | ||
| #endif | ||
| return -1; | ||
| } | ||
|
|
||
| PyObject *old_value; | ||
| sys_set_flag_unlocked(flags, pos, obj, &old_value); | ||
| Py_DECREF(flags); | ||
|
|
||
| // Set PyInterpreterState.long_state.max_str_digits | ||
| // and PyInterpreterState.config.int_max_str_digits. | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| interp->long_state.max_str_digits = maxdigits; | ||
| interp->config.int_max_str_digits = maxdigits; | ||
|
|
||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Unlock(&sys_flags_mutex); | ||
| #endif | ||
|
|
||
| Py_DECREF(obj); | ||
| Py_XDECREF(old_value); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyMutex_Lock() is part of Python.h. There is no need to include the internal pycore_lock.h header.