Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
make local_clear thread-safe
  • Loading branch information
kumaraditya303 committed Jan 10, 2023
commit cd526b1a1668a9c3e42d351b01c4f5410f4de9b2
30 changes: 20 additions & 10 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ local_traverse(localobject *self, visitproc visit, void *arg)
return 0;
}
Comment thread
kumaraditya303 marked this conversation as resolved.

#define HEAD_LOCK(runtime) \
Comment thread
kumaraditya303 marked this conversation as resolved.
PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
#define HEAD_UNLOCK(runtime) \
PyThread_release_lock((runtime)->interpreters.mutex)

static int
local_clear(localobject *self)
{
Expand All @@ -849,18 +854,23 @@ local_clear(localobject *self)
/* Remove all strong references to dummies from the thread states */
if (self->key) {
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyRuntimeState *runtime = &_PyRuntime;
HEAD_LOCK(runtime);
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
for(; tstate; tstate = PyThreadState_Next(tstate)) {
if (tstate->dict == NULL) {
continue;
}
PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
if (v != NULL) {
Py_DECREF(v);
}
else {
PyErr_Clear();
HEAD_UNLOCK(runtime);
while (tstate) {
if (tstate->dict) {
PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
Comment thread
kumaraditya303 marked this conversation as resolved.
if (v != NULL) {
Py_DECREF(v);
}
else {
PyErr_Clear();
}
}
HEAD_LOCK(runtime);
tstate = PyThreadState_Next(tstate);
HEAD_UNLOCK(runtime);
}
}
return 0;
Expand Down