[Python-checkins] bpo-39776: Lock ++interp->tstate_next_unique_id (GH-18746)
Stefan Krah
webhook-mailer at python.org
Tue Mar 3 03:20:02 EST 2020
https://github.com/python/cpython/commit/852aee69f49c654a03ad1f64d90a78ba8848e2c6
commit: 852aee69f49c654a03ad1f64d90a78ba8848e2c6
branch: 3.7
author: Stefan Krah <skrah at bytereef.org>
committer: GitHub <noreply at github.com>
date: 2020-03-03T09:19:58+01:00
summary:
bpo-39776: Lock ++interp->tstate_next_unique_id (GH-18746)
- Threads created by PyGILState_Ensure() could have a duplicate tstate->id.
(cherry picked from commit b3b9ade4a3d3fe00d933bcd8fc5c5c755d1024f9)
files:
A Misc/NEWS.d/next/Core and Builtins/2020-03-02-20-12-33.bpo-39776.fNaxi_.rst
M Python/pystate.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-02-20-12-33.bpo-39776.fNaxi_.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-02-20-12-33.bpo-39776.fNaxi_.rst
new file mode 100644
index 0000000000000..e5a00bd96ae47
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-02-20-12-33.bpo-39776.fNaxi_.rst
@@ -0,0 +1,6 @@
+Fix race condition where threads created by PyGILState_Ensure() could get a
+duplicate id.
+
+This affects consumers of tstate->id like the contextvar caching machinery,
+which could return invalid cached objects under heavy thread load (observed
+in embedded scenarios).
diff --git a/Python/pystate.c b/Python/pystate.c
index 90cd56bb05f4e..ebfe6ce65de03 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -411,12 +411,12 @@ new_threadstate(PyInterpreterState *interp, int init)
tstate->context = NULL;
tstate->context_ver = 1;
- tstate->id = ++interp->tstate_next_unique_id;
if (init)
_PyThreadState_Init(tstate);
HEAD_LOCK();
+ tstate->id = ++interp->tstate_next_unique_id;
tstate->prev = NULL;
tstate->next = interp->tstate_head;
if (tstate->next)
More information about the Python-checkins
mailing list