]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39776: Lock ++interp->tstate_next_unique_id. (GH-18746) (#18746) (#18752)
authorStefan Krah <skrah@bytereef.org>
Tue, 3 Mar 2020 08:18:55 +0000 (09:18 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Mar 2020 08:18:55 +0000 (09:18 +0100)
- Threads created by PyGILState_Ensure() could have a duplicate tstate->id.

(cherry picked from commit b3b9ade4a3d3fe00d933bcd8fc5c5c755d1024f9)

Misc/NEWS.d/next/Core and Builtins/2020-03-02-20-12-33.bpo-39776.fNaxi_.rst [new file with mode: 0644]
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 (file)
index 0000000..e5a00bd
--- /dev/null
@@ -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).
index aba673c00a432233d38700718e3d5797b8b45a5a..9f99060feceb2180f7f9eaf774925c5b7c4a76e1 100644 (file)
@@ -606,13 +606,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(runtime, tstate);
     }
 
     HEAD_LOCK(runtime);
+    tstate->id = ++interp->tstate_next_unique_id;
     tstate->prev = NULL;
     tstate->next = interp->tstate_head;
     if (tstate->next)