Repository navigation
gh-154348: fix data race on nogil build about sys.monitoring.use_tool_id()
#154459
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
base: main
Are you sure you want to change the base?
Changes from all commits
761667a
684f564
ac8f073
59608bc
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 |
|---|---|---|
|
|
@@ -2187,11 +2187,15 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name) | |
| return NULL; | ||
| } | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| PyCriticalSection cs; | ||
| PyCriticalSection_Begin(&cs, module); | ||
| if (interp->monitoring_tool_names[tool_id] != NULL) { | ||
| PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id); | ||
| PyCriticalSection_End(&cs); | ||
| return NULL; | ||
| } | ||
| interp->monitoring_tool_names[tool_id] = Py_NewRef(name); | ||
| PyCriticalSection_End(&cs); | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
|
|
@@ -2213,11 +2217,15 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id) | |
|
|
||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
|
|
||
| PyCriticalSection cs; | ||
| PyCriticalSection_Begin(&cs, module); | ||
|
Comment on lines
+2220
to
+2221
Contributor
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 think we should prefere
Contributor
Author
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. Yes,
Contributor
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. Maybe can use atomics for just a simple read?
Contributor
Author
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. Yes, atomic would be enough. But if we use atomic op here, the writes in other places would also need to be atomic, otherwise TSAN will still report a data race. At that point, it feels like we might not even need critical sections. So I'm inclined to pick either critical sections or atomics rather than mixing the two.
Contributor
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. Do we not need them in sections where we make a write and read to this field? |
||
| if (interp->monitoring_tool_names[tool_id] != NULL) { | ||
| if (_PyMonitoring_ClearToolId(tool_id) < 0) { | ||
| PyCriticalSection_End(&cs); | ||
| return NULL; | ||
| } | ||
| } | ||
| PyCriticalSection_End(&cs); | ||
|
|
||
| Py_RETURN_NONE; | ||
| } | ||
|
|
@@ -2239,13 +2247,20 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id) | |
| } | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
|
|
||
| if (interp->monitoring_tool_names[tool_id] != NULL) { | ||
| bool active; | ||
| Py_BEGIN_CRITICAL_SECTION(module); | ||
| active = (interp->monitoring_tool_names[tool_id] != NULL); | ||
| Py_END_CRITICAL_SECTION(); | ||
|
|
||
| if (active) { | ||
| if (_PyMonitoring_ClearToolId(tool_id) < 0) { | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION(module); | ||
| Py_CLEAR(interp->monitoring_tool_names[tool_id]); | ||
| Py_END_CRITICAL_SECTION(); | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
|
|
@@ -2267,11 +2282,14 @@ monitoring_get_tool_impl(PyObject *module, int tool_id) | |
| return NULL; | ||
| } | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| PyObject *name = interp->monitoring_tool_names[tool_id]; | ||
| PyObject *name; | ||
| Py_BEGIN_CRITICAL_SECTION(module); | ||
| name = Py_XNewRef(interp->monitoring_tool_names[tool_id]); | ||
| Py_END_CRITICAL_SECTION(); | ||
| if (name == NULL) { | ||
| Py_RETURN_NONE; | ||
| } | ||
| return Py_NewRef(name); | ||
| return name; | ||
| } | ||
|
|
||
| /*[clinic input] | ||
|
|
||
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.
this can be refactored so we just have a single critical section exit