Skip to content

Commit 4eb4ae6

Browse files
fix deadlock
1 parent 6087f49 commit 4eb4ae6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Python/pystate.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,16 @@ new_threadstate(PyInterpreterState *interp)
829829
// Every valid interpreter must have at least one thread.
830830
assert(id > 1);
831831
assert(old_head->prev == NULL);
832-
832+
// Unlock before allocating memory.
833+
HEAD_UNLOCK(runtime);
833834
tstate = alloc_threadstate();
835+
HEAD_LOCK(runtime);
834836
if (tstate == NULL) {
835837
goto error;
836838
}
839+
// Read interp->threads.head again as another thread could
840+
// have created a thread state while we were allocating memory.
841+
old_head = interp->threads.head;
837842
// Set to _PyThreadState_INIT.
838843
memcpy(tstate,
839844
&initial._main_interpreter._initial_thread,

0 commit comments

Comments
 (0)