8000 Implement PEP 788 by ZeroIntensity · Pull Request #133110 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Implement PEP 788 #133110

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

Draft
wants to merge 67 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
1828b9a
Implement interpreter state reference counting.
ZeroIntensity Apr 24, 2025
d0895f9
Add a test for interpreter reference counts.
ZeroIntensity Apr 24, 2025
5c3ee8d
Add thread state daemon-ness that doesn't work yet.
ZeroIntensity Apr 25, 2025
7b9ac59
Add untested implementation of non-daemon native threads.
ZeroIntensity Apr 25, 2025
0868c15
Add a test for PyThreadState_SetDaemon().
ZeroIntensity Apr 25, 2025
e9ea644
Add untested implementation of Ensure()/Release() that probably doesn…
ZeroIntensity Apr 25, 2025
0ebdca4
Change some comments.
ZeroIntensity Apr 25, 2025
40989de
Add a test that I'm sure doesn't work.
ZeroIntensity Apr 25, 2025
d501f35
Use the interpreter's reference count and native thread countdown as …
ZeroIntensity Apr 25, 2025
c5ec89c
Fix the countdown decrement.
ZeroIntensity Apr 26, 2025
4e1f599
Remove unused variable.
ZeroIntensity Apr 26, 2025
3127a3f
Test for PyGILState_Ensure()
ZeroIntensity Apr 26, 2025
82b5b9f
Fix the test for the new reference counting.
ZeroIntensity Apr 26, 2025
fda9886
Add PyInterpreterState_Lookup()
ZeroIntensity Apr 26, 2025
f7723c0
Fix a few bugs and add a test.
ZeroIntensity Apr 26, 2025
62e9549
Add a test for PyThreadState_Ensure() across interpreters.
ZeroIntensity Apr 27, 2025
bc60630
Remove an artifact from old approach.
ZeroIntensity Apr 28, 2025
9d8d526
Fix test from earlier semantics.
ZeroIntensity Apr 28, 2025
54b0ce0
Remove 'daemonness' as a property of a thread.
ZeroIntensity May 22, 2025
5955de6
Add strong interpreter reference functions.
ZeroIntensity May 22, 2025
92cf906
Implement weak references.
ZeroIntensity May 22, 2025
b0d0673
Fix some thread safety issues regarding interpreter deletion.
ZeroIntensity May 22, 2025
0a15beb
Merge from main branch.
ZeroIntensity May 22, 2025
16d79de
Implement new version of PyThreadState_Ensure() and PyThreadState_Rel…
ZeroIntensity May 22, 2025
c2bffcd
Use the new APIs in the tests.
ZeroIntensity May 22, 2025
911c6b5
Fix _testcapi.
ZeroIntensity May 22, 2025
b84fa90
Fix the ensure counter.
ZeroIntensity May 22, 2025
9ccf6bd
Add the test to test_embed.
ZeroIntensity May 22, 2025
481caf5
Allow the wait to be interrupted by CTRL+C.
ZeroIntensity May 22, 2025
71e1aec
Print the error before bailing out.
ZeroIntensity May 22, 2025
d661578
Updates for the new proposal.
ZeroIntensity May 28, 2025
4249c5d
Bikeshedding.
ZeroIntensity May 28, 2025
71f2fd7
Apply suggestions from code review
ZeroIntensity May 28, 2025
03ccb38
Fix failing build.
ZeroIntensity May 29, 2025
bca65fb
Rename parameter.
ZeroIntensity May 29, 2025
510ade1
Fix formatting.
ZeroIntensity May 29, 2025
3971408
Add tstate check.
ZeroIntensity May 29, 2025
6c4c52b
Move to pycore_pystate.h
ZeroIntensity May 29, 2025
64920a8
Revert "Move to pycore_pystate.h"
ZeroIntensity May 29, 2025
05436f3
Use an exponential wait time for the event.
ZeroIntensity May 29, 2025
02bc2d7
Mark function as static.
ZeroIntensity May 29, 2025
03fa2af
Update fatal error message.
ZeroIntensity May 29, 2025
b955d85
Remove incorrect assertion.
ZeroIntensity May 29, 2025
5236700
Add a comment regarding PyMem_RawMalloc()
ZeroIntensity May 29, 2025
a277130
Add a comment.
ZeroIntensity Jun 1, 2025
dac0c1a
Update Include/cpython/pystate.h
ZeroIntensity Jun 1, 2025
ab9e3b5
Merge branch 'pep-788-impl' of https://github.com/ZeroIntensity/cpyth…
ZeroIntensity Jun 1, 2025
79a1852
Move some tests around to prevent exposure of the private API.
ZeroIntensity Jun 1, 2025
47957b8
Move weakref test to internal C API.
ZeroIntensity Jun 1, 2025
771d7ed
Improve reference counting tests.
ZeroIntensity Jun 1, 2025
0c3c1c7
Remove dead function.
ZeroIntensity Jun 1, 2025
531928e
Add some more tests.
ZeroIntensity Jun 1, 2025
08a8af6
Remove unused variables.
ZeroIntensity Jun 1, 2025
02f93bc
Fix some thread state attachment problems.
ZeroIntensity Jun 1, 2025
d6c82bd
Only delete thread states created by PyThreadState_Ensure()
ZeroIntensity Jun 1, 2025
082fd69
Add a test for crossinterpreter ensures.
ZeroIntensity Jun 1, 2025
61f70ae
Add a test for weak interpreter references.
ZeroIntensity Jun 1, 2025
fa961e9
Fix concurrent shutdown races in PyGILState_Ensure().
ZeroIntensity Jun 1, 2025
ea1da77
Add a test for PyInterpreterRef_Main().
ZeroIntensity Jun 1, 2025
6f19384
Use PyErr_FormatUnraisable to show signals.
ZeroIntensity Jun 1, 2025
b702da2
Fix a re-entrancy deadlock.
ZeroIntensity Jun 1, 2025
40e7e68
Remove stupid IDE imports.
ZeroIntensity Jun 1, 2025
96efc81
Merge branch 'main' of https://github.com/python/cpython into pep-788…
ZeroIntensity Jun 21, 2025
af6f6fd
Merge branch 'pep-788-impl' of https://github.com/ZeroIntensity/cpyth…
ZeroIntensity Jun 21, 2025
2c52cdc
Fix interpreter reference count tests.
ZeroIntensity Jun 21, 2025
3ffe9d0
Merge branch 'main' of https://github.com/python/cpython into pep-788…
ZeroIntensity Jul 9, 2025
588364c
Add thread state references to PyThreadState_Ensure() and
ZeroIntensity Jul 9, 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
Prev Previous commit
Next Next commit
Updates for the new proposal.
  • Loading branch information
ZeroIntensity committed May 28, 2025
commit d66157883d854eabef0d555d2d8f5b15ae905312
19 changes: 13 additions & 6 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,32 +270,39 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(

typedef uintptr_t PyInterpreterRef;

PyAPI_FUNC(PyInterpreterRef) PyInterpreterRef_Get(void);
PyAPI_FUNC(int) PyInterpreterRef_Get(PyInterpreterRef *ptr);
PyAPI_FUNC(PyInterpreterRef) PyInterpreterRef_Dup(PyInterpreterRef ref);
PyAPI_FUNC(int) PyInterpreterState_AsStrong(PyInterpreterState *interp, PyInterpreterRef *strong_ptr);
PyAPI_FUNC(int) PyInterpreterRef_Main(PyInterpreterRef *strong_ptr);
PyAPI_FUNC(void) PyInterpreterRef_Close(PyInterpreterRef ref);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterRef_AsInterpreter(PyInterpreterRef ref);

#define PyInterpreterRef_Close(ref) do { \
PyInterpreterRef_Close(ref); \
ref = 0; \
} while (0); \
} while (0);

/* Weak interpreter references */

typedef struct _interpreter_weakref {
int64_t id;
Py_ssize_t refcount;
} PyInterpreterWeakRef;
} _PyInterpreterWeakRef;

PyAPI_FUNC(PyInterpreterWeakRef) PyInterpreterWeakRef_Get(void);
typedef _PyInterpreterWeakRef *PyInterpreterWeakRef;

PyAPI_FUNC(int) PyInterpreterWeakRef_Get(PyInterpreterWeakRef *ptr);
PyAPI_FUNC(PyInterpreterWeakRef) PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref);
PyAPI_FUNC(int) PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *strong_ptr);
PyAPI_FUNC(void) PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref);

#define PyInterpreterWeakRef_Close(ref) do { \
PyInterpreterWeakRef_Close(ref); \
ref = 0; \
} while (0);

// Exports for '_testcapi' shared extension
PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_Refcount(PyInterpreterState *interp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move it to the internal C API? (Include/internal/pycore_pystate.h).

Same remark for _PyInterpreterState_Incref().

Copy link
Member Author
@ZeroIntensity ZeroIntensity May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_testcapi doesn't allow importing from pycore headers. I only need these for those tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move tests to _testinternalcapi in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we're not testing the internal C API, we're testing the public C API. We just use the internal C API to write those tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add private functions (ex: _PyInterpreterState_Refcount()) to the public C API. So move it to the internal C API. Sadly, it means moving your tests to the internal C API tests, unless a test only uses public functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split up the tests, so now the internal stuff is in _testinternalcapi and the internal functions are in the private headers.

PyAPI_FUNC(void) _PyInterpreterState_Incref(PyInterpreterState *interp);
PyAPI_FUNC(int) _PyInterpreterState_Incref(PyInterpreterState *interp);

PyAPI_FUNC(int) PyThreadState_Ensure(PyInterpreterRef interp_ref);

Expand Down
25 changes: 19 additions & 6 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,16 @@ toggle_reftrace_printer(PyObject *ob, PyObject *arg)
Py_RETURN_NONE;
}

static PyInterpreterRef
get_strong_ref(void)
{
PyInterpreterRef ref;
if (PyInterpreterRef_Get(&ref) < 0) {
Py_FatalError("strong reference should not have failed");
}
return ref;
}

static PyObject *
test_interp_refcount(PyObject *self, PyObject *unused)
{
Expand All @@ -2555,16 +2565,16 @@ test_interp_refcount(PyObject *self, PyObject *unused)

// Reference counts are technically 0 by default
assert(_PyInterpreterState_Refcount(interp) == 0);
ref1 = PyInterpreterRef_Get();
ref1 = get_strong_ref();
assert(_PyInterpreterState_Refcount(interp) == 1);
ref2 = PyInterpreterRef_Get();
ref2 = get_strong_ref();
assert(_PyInterpreterState_Refcount(interp) == 2);
PyInterpreterRef_Close(ref1);
assert(_PyInterpreterState_Refcount(interp) == 1);
PyInterpreterRef_Close(ref2);
assert(_PyInterpreterState_Refcount(interp) == 0);

ref1 = PyInterpreterRef_Get();
ref1 = get_strong_ref();
ref2 = PyInterpreterRef_Dup(ref1);
assert(_PyInterpreterState_Refcount(interp) == 2);
assert(PyInterpreterRef_AsInterpreter(ref1) == interp);
Expand All @@ -2580,7 +2590,10 @@ static PyObject *
test_interp_weak_ref(PyObject *self, PyObject *unused)
{
PyInterpreterState *interp = PyInterpreterState_Get();
PyInterpreterWeakRef wref = PyInterpreterWeakRef_Get();
PyInterpreterWeakRef wref;
if (PyInterpreterWeakRef_Get(&wref) < 0) {
return NULL;
}
assert(_PyInterpreterState_Refcount(interp) == 0);

PyInterpreterRef ref;
Expand All @@ -2598,10 +2611,10 @@ static PyObject *
test_interp_ensure(PyObject *self, PyObject *unused)
{
PyInterpreterState *interp = PyInterpreterState_Get();
PyInterpreterRef ref = PyInterpreterRef_Get();
PyInterpreterRef ref = get_strong_ref();
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
PyThreadState *tstate = Py_NewInterpreter();
PyInterpreterRef sub_ref = PyInterpreterRef_Get();
PyInterpreterRef sub_ref = get_strong_ref();
PyInterpreterState *subinterp = PyThreadState_GetInterpreter(tstate);

for (int i = 0; i < 10; ++i) {
Expand Down
5 changes: 4 additions & 1 deletion Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,10 @@ test_thread_state_ensure(void)
_testembed_initialize();
PyThread_handle_t handle;
PyThread_ident_t ident;
PyInterpreterRef ref = PyInterpreterRef_Get();
PyInterpreterRef ref;
if (PyInterpreterRef_Get(&ref) < 0) {
return -1;
};
ThreadData data = { ref };
if (PyThread_start_joinable_thread(do_tstate_ensure, &data,
&ident, &handle) < 0) {
Expand Down
80 changes: 62 additions & 18 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -3145,12 +3145,22 @@ _PyInterpreterState_Refcount(PyInterpreterState *interp)
return refcount;
}

void
int
_PyInterpreterState_Incref(PyInterpreterState *interp)
{
assert(interp != NULL);
assert(_Py_atomic_load_ssize_relaxed(&interp->threads.finalizing.countdown) >= 0);
struct _Py_finalizing_threads *finalizing = &interp->threads.finalizing;
assert(_Py_atomic_load_ssize_relaxed(&finalizing->countdown) >= 0);
PyMutex *mutex = &finalizing->mutex;
PyMutex_Lock(mutex);
if (_PyEvent_IsSet(&finalizing->finished)) {
PyMutex_Unlock(mutex);
return -1;
}

_Py_atomic_add_ssize(&interp->threads.finalizing.countdown, 1);
PyMutex_Unlock(mutex);
return 0;
}

static PyInterpreterState *
Expand All @@ -3164,19 +3174,29 @@ ref_as_interp(PyInterpreterRef ref)
return interp;
}

PyInterpreterRef
PyInterpreterRef_Get(void)
int
PyInterpreterRef_Get(PyInterpreterRef *ref_ptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may rename the parameter to just "ref".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
assert(ref_ptr != NULL);
PyInterpreterState *interp = PyInterpreterState_Get();
_PyInterpreterState_Incref(interp);
return (PyInterpreterRef)interp;
if (_PyInterpreterState_Incref(interp) < 0) {
PyErr_SetString(PyExc_PythonFinalizationError,
"Cannot acquire strong interpreter references anymore");
return -1;
}
*ref_ptr = (PyInterpreterRef)interp;
return 0;
}

PyInterpreterRef
PyInterpreterRef_Dup(PyInterpreterRef ref)
{
PyInterpreterState *interp = ref_as_interp(ref);
_PyInterpreterState_Incref(interp);
int res = _PyInterpreterState_Incref(interp);
(void)res;
// We already hold a strong reference, so it shouldn't be possible
// for the interpreter to be at a point where references don't work anymore
assert(res == 0);
return (PyInterpreterRef)interp;
}

Expand All @@ -3195,24 +3215,48 @@ PyInterpreterRef_AsInterpreter(PyInterpreterRef ref)
return interp;
}

PyInterpreterWeakRef
PyInterpreterWeakRef_Get(void)
int
PyInterpreterWeakRef_Get(PyInterpreterWeakRef *wref_ptr)
{
PyInterpreterState *interp = PyInterpreterState_Get();
PyInterpreterWeakRef wref = { interp->id };
_PyInterpreterWeakRef *wref = PyMem_RawMalloc(sizeof(_PyInterpreterWeakRef));
if (wref == NULL) {
PyErr_NoMemory();
return -1;
}
wref->refcount = 1;
wref->id = interp->id;
*wref_ptr = (PyInterpreterWeakRef)wref;
return 0;
}

static _PyInterpreterWeakRef *
wref_handle_as_ptr(PyInterpreterWeakRef wref_handle)
{
_PyInterpreterWeakRef *wref = (_PyInterpreterWeakRef *)wref_handle;
if (wref == NULL) {
Py_FatalError("Got a null weak interpreter reference, likely due to use after close.");
}

return wref;
}

PyInterpreterWeakRef
PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref)
PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref_handle)
{
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle);
++wref->refcount;
return wref;
}

#undef PyInterpreterWeakRef_Close
void
PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref)
PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref_handle)
{
return;
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle);
if (--wref->refcount == 0) {
PyMem_RawFree(wref);
}
}

static int
Expand All @@ -3234,10 +3278,11 @@ try_acquire_strong_ref(PyInterpreterState *interp, PyInterpreterRef *strong_ptr)
}

int
PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *strong_ptr)
PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref_handle, PyInterpreterRef *strong_ptr)
{
assert(strong_ptr != NULL);
int64_t interp_id = wref.id;
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle);
int64_t interp_id = wref->id;
/* Interpreters cannot be deleted while we hold the runtime lock. */
_PyRuntimeState *runtime = &_PyRuntime;
HEAD_LOCK(runtime);
Expand All @@ -3254,13 +3299,12 @@ PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *stron
}

int
PyInterpreterState_AsStrong(PyInterpreterState *interp, PyInterpreterRef *strong_ptr)
PyInterpreterRef_Main(PyInterpreterRef *strong_ptr)
{
assert(interp != NULL);
assert(strong_ptr != NULL);
_PyRuntimeState *runtime = &_PyRuntime;
HEAD_LOCK(runtime);
int res = try_acquire_strong_ref(interp, strong_ptr);
int res = try_acquire_strong_ref(&runtime->_main_interpreter, strong_ptr);
HEAD_UNLOCK(runtime);

return res;
Expand Down
Loading
0