bpo-39776: Lock ++interp->tstate_next_unique_id (GH-18746) · python/cpython@852aee6 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 +Fix race condition where threads created by PyGILState_Ensure() could get a
2 +duplicate id.
3 +
4 +This affects consumers of tstate->id like the contextvar caching machinery,
5 +which could return invalid cached objects under heavy thread load (observed
6 +in embedded scenarios).
Original file line number Diff line number Diff line change
@@ -411,12 +411,12 @@ new_threadstate(PyInterpreterState *interp, int init)
411 411 tstate->context = NULL;
412 412 tstate->context_ver = 1;
413 413
414 -tstate->id = ++interp->tstate_next_unique_id;
415 414
416 415 if (init)
417 416 _PyThreadState_Init(tstate);
418 417
419 418 HEAD_LOCK();
419 +tstate->id = ++interp->tstate_next_unique_id;
420 420 tstate->prev = NULL;
421 421 tstate->next = interp->tstate_head;
422 422 if (tstate->next)