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

Merged
merged 27 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4652f0d
Convert the gilstate into a thread-local.
ZeroIntensity Apr 14, 2025
0e93b03
Add a comment.
ZeroIntensity Apr 14, 2025
ba4c00e
Add a test case.
ZeroIntensity Apr 14, 2025
2e99092
Fix the test.
ZeroIntensity Apr 14, 2025
6425c3e
Fix the test case (again).
ZeroIntensity Apr 14, 2025
78cd775
Remove unused variable.
ZeroIntensity Apr 14, 2025
c8421fc
Add blurb.
ZeroIntensity Apr 14, 2025
0a875ff
Make the C analyzer happy.
ZeroIntensity Apr 14, 2025
dd330f8
Fix the C analyzer (round 2).
ZeroIntensity Apr 14, 2025
a3c2292
Add test to test_embed
ZeroIntensity Apr 15, 2025
86be7f3
Update Misc/NEWS.d/next/C_API/2025-04-14-07-41-28.gh-issue-131185.ZCj…
ZeroIntensity Apr 15, 2025
453a468
Fix some of the comments.
ZeroIntensity Apr 15, 2025
da9b606
Use PyEvent instead of _PySemaphore
ZeroIntensity Apr 15, 2025
55be81c
Merge branch 'gilstate-thread-local' of https://github.com/ZeroIntens…
ZeroIntensity Apr 15, 2025
2f9e499
Fix scalar initializer.
ZeroIntensity Apr 18, 2025
0dba178
Add comment about thread-safety.
ZeroIntensity Apr 18, 2025
863d2bc
Fix header.
ZeroIntensity May 18, 2025
bbf8aa4
Merge branch 'main' of https://github.com/python/cpython into gilstat…
ZeroIntensity May 18, 2025
cad0404
Fix renaming.
ZeroIntensity May 18, 2025
779be6e
Restore assertion.
ZeroIntensity May 18, 2025
86144fc
Clear the thread-local.
ZeroIntensity May 19, 2025
9f1d919
Restore trashTSSkey silliness.
ZeroIntensity May 19, 2025
79096c8
Merge branch 'main' of https://github.com/python/cpython into gilstat…
ZeroIntensity May 19, 2025
0c16fea
Fix broken build.
ZeroIntensity May 19, 2025
7c27c9f
Revert "Clear the thread-local."
ZeroIntensity May 19, 2025
8f18e95
Reapply "Clear the thread-local."
ZeroIntensity May 19, 2025
f90bb11
Only remove the evil gilstate clear.
ZeroIntensity May 20, 2025
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
5 changes: 1 addition & 4 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 Expand Up @@ -236,4 +233,4 @@ extern PyTypeObject _PyExc_MemoryError;
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_RUNTIME_INIT_H */
#endif /* !Py_INTERNAL_RUNTIME_INIT_H */
3 changes: 0 additions & 3 deletions Include/internal/pycore_runtime_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +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;

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 @@ -1915,6 +1915,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_lock.h" // PyEvent
#include "pycore_pythread.h" // PyThread_start_joinable_thread()
#include "pycore_import.h" // _PyImport_FrozenBootstrap
#include <inttypes.h>
Expand Down Expand Up @@ -2315,6 +2316,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_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 @@ -2405,7 +2432,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