From 378210fc2aa522979a3666d485fd1232e2efddc6 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 8 Nov 2022 16:31:26 -0800 Subject: [PATCH 1/4] Mark new interpreters and threads as non-static --- Python/pystate.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index dd6d6e92eca89a..af54426fd4a597 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -356,6 +356,7 @@ PyInterpreterState_New(void) interp = &runtime->_main_interpreter; assert(interp->id == 0); assert(interp->next == NULL); + assert(interp->_static); interpreters->main = interp; } @@ -368,8 +369,8 @@ PyInterpreterState_New(void) goto error; } // Set to _PyInterpreterState_INIT. - memcpy(interp, &initial._main_interpreter, - sizeof(*interp)); + memcpy(interp, &initial._main_interpreter, sizeof(*interp)); + interp->_static = false; if (id < 0) { /* overflow or Py_Initialize() not called yet! */ @@ -837,6 +838,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. @@ -848,6 +850,7 @@ new_threadstate(PyInterpreterState *interp) memcpy(tstate, &initial._main_interpreter._initial_thread, sizeof(*tstate)); + tstate->_static = false; } interp->threads.head = tstate; From 287155b6fbefb827cbcd161ff7b87b0c2238ff6a Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 8 Nov 2022 16:35:37 -0800 Subject: [PATCH 2/4] blurb add --- .../2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst 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 index 00000000000000..8ad0e147c20351 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-11-08-16-35-25.gh-issue-99205.2YOoFT.rst @@ -0,0 +1,2 @@ +Fix an issue that prevented :c:type:`PyThreadState` and +:c:type:`PyInterpreterState` memory from being freed properly. From 5171ab1a23e4df058a580f4cd2427e9d788f810e Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 8 Nov 2022 16:39:10 -0800 Subject: [PATCH 3/4] fixup --- Python/pystate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index af54426fd4a597..667aaa04988878 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -369,7 +369,8 @@ PyInterpreterState_New(void) goto error; } // Set to _PyInterpreterState_INIT. - memcpy(interp, &initial._main_interpreter, sizeof(*interp)); + memcpy(interp, &initial._main_interpreter, + sizeof(*interp)); interp->_static = false; if (id < 0) { From f380af050d5d245f4310468455a22f22a9248dc8 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Wed, 9 Nov 2022 13:04:21 -0800 Subject: [PATCH 4/4] Feedback from code review --- Python/pystate.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index 667aaa04988878..df0f4e2a166f6f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -371,6 +371,8 @@ 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) { @@ -851,6 +853,8 @@ 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;