]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-99205: Mark new interpreters and threads as non-static (GH-99268)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 9 Nov 2022 22:19:15 +0000 (14:19 -0800)
committerGitHub <noreply@github.com>
Wed, 9 Nov 2022 22:19:15 +0000 (14:19 -0800)
(cherry picked from commit 283ab0e1c002f2d7459d581df6b4b8599e7d1a4d)

Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Misc/NEWS.d/next/Core and Builtins/2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst [new file with mode: 0644]
Python/pystate.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst b/Misc/NEWS.d/next/Core and Builtins/2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst
new file mode 100644 (file)
index 0000000..8ad0e14
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an issue that prevented :c:type:`PyThreadState` and
+:c:type:`PyInterpreterState` memory from being freed properly.
index 425065322ebd429d24325eed33f59f21332238a9..c0d161f894c974ead04a61134a9880b66e89d883 100644 (file)
@@ -341,6 +341,7 @@ PyInterpreterState_New(void)
         interp = &runtime->_main_interpreter;
         assert(interp->id == 0);
         assert(interp->next == NULL);
+        assert(interp->_static);
 
         interpreters->main = interp;
     }
@@ -355,6 +356,9 @@ PyInterpreterState_New(void)
         // Set to _PyInterpreterState_INIT.
         memcpy(interp, &initial._main_interpreter,
                sizeof(*interp));
+        // We need to adjust any fields that are different from the initial
+        // interpreter (as defined in _PyInterpreterState_INIT):
+        interp->_static = false;
 
         if (id < 0) {
             /* overflow or Py_Initialize() not called yet! */
@@ -817,6 +821,7 @@ new_threadstate(PyInterpreterState *interp)
         assert(id == 1);
         used_newtstate = 0;
         tstate = &interp->_initial_thread;
+        assert(tstate->_static);
     }
     else {
         // Every valid interpreter must have at least one thread.
@@ -828,6 +833,9 @@ new_threadstate(PyInterpreterState *interp)
         memcpy(tstate,
                &initial._main_interpreter._initial_thread,
                sizeof(*tstate));
+        // We need to adjust any fields that are different from the initial
+        // thread (as defined in _PyThreadState_INIT):
+        tstate->_static = false;
     }
     interp->threads.head = tstate;