Skip to content

Commit 466c0d9

Browse files
Lock "HEAD" around looking up intepreter by ID.
1 parent 416c522 commit 466c0d9

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

Python/pystate.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,38 @@ PyInterpreterState_GetID(PyInterpreterState *interp)
328328

329329

330330
PyInterpreterState *
331-
_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
331+
interp_lookup_id(PY_INT64_T requested_id)
332332
{
333-
if (requested_id < 0)
334-
goto error;
335-
336333
PyInterpreterState *interp = PyInterpreterState_Head();
337334
while (interp != NULL) {
338335
PY_INT64_T id = PyInterpreterState_GetID(interp);
339-
if (id < 0)
336+
if (id < 0) {
340337
return NULL;
341-
if (requested_id == id)
338+
}
339+
if (requested_id == id) {
342340
return interp;
341+
}
343342
interp = PyInterpreterState_Next(interp);
344343
}
345-
346-
error:
347-
PyErr_Format(PyExc_RuntimeError,
348-
"unrecognized interpreter ID %lld", requested_id);
349344
return NULL;
350345
}
351346

347+
PyInterpreterState *
348+
_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
349+
{
350+
PyInterpreterState *interp = NULL;
351+
if (requested_id >= 0) {
352+
HEAD_UNLOCK();
353+
interp = interp_lookup_id(requested_id);
354+
HEAD_UNLOCK();
355+
}
356+
if (interp == NULL && !PyErr_Occurred()) {
357+
PyErr_Format(PyExc_RuntimeError,
358+
"unrecognized interpreter ID %lld", requested_id);
359+
}
360+
return interp;
361+
}
362+
352363

353364
int
354365
_PyInterpreterState_IDInitref(PyInterpreterState *interp)

0 commit comments

Comments
 (0)