Skip to content

Commit f9becec

Browse files
committed
_PyGILState_Init(), PyGILState_Ensure(): Since PyThread_set_key_value()
can fail, check its return value, and die if it does fail. _PyGILState_Init(): Assert that the thread doesn't already have an association for autoTLSkey. If it does, PyThread_set_key_value() will ignore the attempt to (re)set the association, which the code clearly doesn't want.
1 parent fda787f commit f9becec

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

Python/pystate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
395395
autoTLSkey = PyThread_create_key();
396396
autoInterpreterState = i;
397397
/* Now stash the thread state for this thread in TLS */
398-
PyThread_set_key_value(autoTLSkey, (void *)t);
398+
assert(PyThread_get_key_value(autoTLSkey) == NULL);
399+
if (PyThread_set_key_value(autoTLSkey, (void *)t) < 0)
400+
Py_FatalError("Couldn't create autoTLSkey mapping");
399401
assert(t->gilstate_counter == 0); /* must be a new thread state */
400402
t->gilstate_counter = 1;
401403
}
@@ -434,7 +436,8 @@ PyGILState_Ensure(void)
434436
tcur = PyThreadState_New(autoInterpreterState);
435437
if (tcur == NULL)
436438
Py_FatalError("Couldn't create thread-state for new thread");
437-
PyThread_set_key_value(autoTLSkey, (void *)tcur);
439+
if (PyThread_set_key_value(autoTLSkey, (void *)tcur) < 0)
440+
Py_FatalError("Couldn't create autoTLSkey mapping");
438441
current = 0; /* new thread state is never current */
439442
}
440443
else

0 commit comments

Comments
 (0)