Repository navigation
Conversation
|
I discussed this with @gaogaotiantian , and he'd like to see if there are approaches other than a lock to handle the data race. I've put together a few options:
#define LOCK_CODE(code) \
assert(!_PyInterpreterState_GET()->stoptheworld.world_stopped); \
Py_BEGIN_CRITICAL_SECTION(code)
|
|
This PR is stale because it has been open for 90 days with no activity. |
|
Do you have an implementation with critical section to review? I think we can deadlock with the plain mutex because we can technically execute arbitrary Python code on |
|
@johng Thanks! That makes sense. I've switched to critical section |
| PyCriticalSection cs; | ||
| PyCriticalSection_Begin(&cs, module); |
There was a problem hiding this comment.
I think we should prefere Py_BEGIN_CRITICAL_SECTION which gives added benefit of fork safety and is compiled out for the GIL build
There was a problem hiding this comment.
Yes, Py_BEGIN_CRITICAL_SECTION is indeed better, though using it also requires restructuring the NULL check so there's a single critical section exit, which makes the diff a bit larger. My original intention was to keep the changes minimal to make the PR easier to review, though I'm not sure which approach is better here.
| } | ||
| return Py_NewRef(name); | ||
| PyObject *ret = Py_NewRef(name); | ||
| PyCriticalSection_End(&cs); |
There was a problem hiding this comment.
can the unlock not be closer to line 2284?
There was a problem hiding this comment.
I've advanced the unlock, but I'm afraid it still needs to be placed within the critical section, because a concurrent free_tool_id can run Py_CLEAR in between, turning the Py_NewRef into a use-after-free.
| 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); |
There was a problem hiding this comment.
this can be refactored so we just have a single critical section exit
| PyCriticalSection cs; | ||
| PyCriticalSection_Begin(&cs, module); | ||
| if (interp->monitoring_tool_names[tool_id] != NULL) { | ||
| if (_PyMonitoring_ClearToolId(tool_id) < 0) { | ||
| PyCriticalSection_End(&cs); |
There was a problem hiding this comment.
I think this can be split across two critical sections so we're not holding it over the clear. We'd never have certainty we're holding it for the whole section anyway
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
|
|
||
| PyCriticalSection cs; | ||
| PyCriticalSection_Begin(&cs, module); |
There was a problem hiding this comment.
Maybe can use atomics for just a simple read?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Do we not need them in sections where we make a write and read to this field?
|
@gaogaotiantian would you kindly be able to help review too as was mentioned above you had previous discussion on this issue, thanks! |
As reported in #154348 (found by devdanzin's fusil),
interp->monitoring_tool_names[tool_id]forsys.monitoringis accessed without any synchronization, which is a data race on free-threaded build.The fix is straightforward, just add a dedicated
PyMutexand serialize all access onmonitoring_tool_namesTesting
Notes for reviewer:
monitoring.get_tool) needs one as well? Because it reads the pointer and then increfs it. A concurrentfree_tool_idcan runPy_CLEAR(drop the last reference and free the object) in between, turning thePy_NewRefinto a use-after-free._PyMonitoring_ClearToolId, which internally does a stop-the-world. Is it safe to hold a PyMutex across STW? This is safe, because a thread blocked onPyMutex_Lockdetaches its thread state, so it is seen as stopped.tool_idinsys.monitoring#154348