Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89c0ec9

Browse files
committed
Revert rev 2.35. It was based on erroneous reasoning -- the current
thread's id can't get duplicated, because (of course!) the current thread is still running. The code should work either way, but reverting the gratuitous change should make backporting easier, and gets the bad reasoning out of 2.35's new comments.
1 parent 8470558 commit 89c0ec9

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

Python/pystate.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,24 @@ PyGILState_Release(PyGILState_STATE oldstate)
484484
assert(tcur->gilstate_counter >= 0); /* illegal counter value */
485485

486486
/* If we are about to destroy this thread-state, we must
487-
* clear it while the lock is held, as destructors may run.
488-
* In addition, we have to delete out TLS entry, which is keyed
489-
* by thread id, while the GIL is held: the thread calling us may
490-
* go away, and a new thread may be created with the same thread
491-
* id. If we don't delete our TLS until after the GIL is released,
492-
* that new thread may manage to insert a TLS value with the same
493-
* thread id as ours, and then we'd erroneously delete it.
494-
*/
487+
clear it while the lock is held, as destructors may run
488+
*/
495489
if (tcur->gilstate_counter == 0) {
496490
/* can't have been locked when we created it */
497491
assert(oldstate == PyGILState_UNLOCKED);
498492
PyThreadState_Clear(tcur);
499-
/* Delete this thread from our TLS */
500-
PyThread_delete_key_value(autoTLSkey);
501493
}
502494

503495
/* Release the lock if necessary */
504496
if (oldstate == PyGILState_UNLOCKED)
505497
PyEval_ReleaseThread(tcur);
506498

507-
/* Now complete destruction of the thread if necessary. This
508-
* couldn't be done before PyEval_ReleaseThread() because
509-
* PyThreadState_Delete doesn't allow deleting the current thread.
510-
*/
511-
if (tcur->gilstate_counter == 0)
499+
/* Now complete destruction of the thread if necessary */
500+
if (tcur->gilstate_counter == 0) {
501+
/* Delete this thread from our TLS */
502+
PyThread_delete_key_value(autoTLSkey);
503+
/* Delete the thread-state */
512504
PyThreadState_Delete(tcur);
505+
}
513506
}
514507
#endif /* WITH_THREAD */

0 commit comments

Comments
 (0)