PyType_AddWatcher and PyType_ClearWatcher are not thread safe as it modifies the interp's type watchers non-atomically.
PyType_AddWatcher:
|
PyType_AddWatcher(PyType_WatchCallback callback) |
|
{ |
|
PyInterpreterState *interp = _PyInterpreterState_GET(); |
|
|
|
// start at 1, 0 is reserved for cpython optimizer |
|
for (int i = 1; i < TYPE_MAX_WATCHERS; i++) { |
|
if (!interp->type_watchers[i]) { |
|
interp->type_watchers[i] = callback; |
|
return i; |
|
} |
|
} |
|
|
|
PyErr_SetString(PyExc_RuntimeError, "no more type watcher IDs available"); |
|
return -1; |
|
} |
PyType_ClearWatcher:
|
PyType_ClearWatcher(int watcher_id) |
|
{ |
|
PyInterpreterState *interp = _PyInterpreterState_GET(); |
|
if (validate_watcher_id(interp, watcher_id) < 0) { |
|
return -1; |
|
} |
|
interp->type_watchers[watcher_id] = NULL; |
|
return 0; |
|
} |
|
|
I think adding and removing of type watchers is a rare event so maybe instead of adding atomics or locks it would be better to change them to use stop-the-world pause event.
Linked PRs
PyType_AddWatcherandPyType_ClearWatcherare not thread safe as it modifies the interp's type watchers non-atomically.PyType_AddWatcher:cpython/Objects/typeobject.c
Lines 949 to 963 in d3f6063
PyType_ClearWatcher:cpython/Objects/typeobject.c
Lines 980 to 989 in d3f6063
I think adding and removing of type watchers is a rare event so maybe instead of adding atomics or locks it would be better to change them to use stop-the-world pause event.
Linked PRs