Skip to content

gh-131185: Use a proper thread-local for cached thread states #132510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ extern PyTypeObject _PyExc_MemoryError;
}, \
}, \
}, \
/* A TSS key must be initialized with Py_tss_NEEDS_INIT \
in accordance with the specification. */ \
.autoTSSkey = Py_tss_NEEDS_INIT, \
.parser = _parser_runtime_state_INIT, \
.ceval = { \
.pending_mainthread = { \
Expand Down
6 changes: 0 additions & 6 deletions Include/internal/pycore_runtime_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ struct pyruntimestate {
struct _pythread_runtime_state threads;
struct _signals_runtime_state signals;

/* Used for the thread state bound to the current thread. */
Py_tss_t autoTSSkey;

/* Used instead of PyThreadState.trash when there is not current tstate. */
Py_tss_t trashTSSkey;

PyWideStringList orig_argv;

struct _parser_runtime_state parser;
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,10 @@ def test_get_incomplete_frame(self):
self.run_embedded_interpreter("test_get_incomplete_frame")


def test_gilstate_after_finalization(self):
self.run_embedded_interpreter("test_gilstate_after_finalization")


class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
def test_unicode_id_init(self):
# bpo-42882: Test that _PyUnicode_FromId() works
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:c:func:`PyGILState_Ensure` no longer crashes when called after interpreter
finalization.
29 changes: 28 additions & 1 deletion Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Python.h>
#include "pycore_initconfig.h" // _PyConfig_InitCompatConfig()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_semaphore.h" // _PySemaphore
#include "pycore_pythread.h" // PyThread_start_joinable_thread()
#include "pycore_import.h" // _PyImport_FrozenBootstrap
#include <inttypes.h>
Expand Down Expand Up @@ -2341,6 +2342,32 @@ test_get_incomplete_frame(void)
return result;
}

static void
do_gilstate_ensure(void *event_ptr)
{
PyEvent *event = (PyEvent *)event_ptr;
// Signal to the calling thread that we've started
_PyEvent_Notify(event);
PyGILState_Ensure(); // This should hang
assert(NULL);
}

static int
test_gilstate_after_finalization(void)
{
_testembed_Py_Initialize();
Py_Finalize();
PyThread_handle_t handle;
PyThread_ident_t ident;
PyEvent event = {0};
if (PyThread_start_joinable_thread(&do_gilstate_ensure, &event, &ident, &handle) < 0) {
return -1;
}
PyEvent_Wait(&event);
// We're now pretty confident that the thread went for
// PyGILState_Ensure(), but that means it got hung.
return PyThread_detach_thread(handle);
}

/* *********************************************************
* List of test cases and the function that implements it.
Expand Down Expand Up @@ -2431,7 +2458,7 @@ static struct TestCase TestCases[] = {
{"test_frozenmain", test_frozenmain},
#endif
{"test_get_incomplete_frame", test_get_incomplete_frame},

{"test_gilstate_after_finalization", test_gilstate_after_finalization},
{NULL, NULL}
};

Expand Down
Loading
Loading