summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval_gil.c28
-rw-r--r--Python/pystate.c29
2 files changed, 32 insertions, 25 deletions
diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c
index cef5317..3b7e6cb 100644
--- a/Python/ceval_gil.c
+++ b/Python/ceval_gil.c
@@ -329,28 +329,6 @@ drop_gil(struct _ceval_state *ceval, PyThreadState *tstate)
}
-/* Check if a Python thread must exit immediately, rather than taking the GIL
- if Py_Finalize() has been called.
-
- When this function is called by a daemon thread after Py_Finalize() has been
- called, the GIL does no longer exist.
-
- tstate must be non-NULL. */
-static inline int
-tstate_must_exit(PyThreadState *tstate)
-{
- /* bpo-39877: Access _PyRuntime directly rather than using
- tstate->interp->runtime to support calls from Python daemon threads.
- After Py_Finalize() has been called, tstate can be a dangling pointer:
- point to PyThreadState freed memory. */
- PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
- if (finalizing == NULL) {
- finalizing = _PyInterpreterState_GetFinalizing(tstate->interp);
- }
- return (finalizing != NULL && finalizing != tstate);
-}
-
-
/* Take the GIL.
The function saves errno at entry and restores its value at exit.
@@ -366,7 +344,7 @@ take_gil(PyThreadState *tstate)
// XXX It may be more correct to check tstate->_status.finalizing.
// XXX assert(!tstate->_status.cleared);
- if (tstate_must_exit(tstate)) {
+ if (_PyThreadState_MustExit(tstate)) {
/* bpo-39877: If Py_Finalize() has been called and tstate is not the
thread which called Py_Finalize(), exit immediately the thread.
@@ -404,7 +382,7 @@ take_gil(PyThreadState *tstate)
_Py_atomic_load_relaxed(&gil->locked) &&
gil->switch_number == saved_switchnum)
{
- if (tstate_must_exit(tstate)) {
+ if (_PyThreadState_MustExit(tstate)) {
MUTEX_UNLOCK(gil->mutex);
// gh-96387: If the loop requested a drop request in a previous
// iteration, reset the request. Otherwise, drop_gil() can
@@ -444,7 +422,7 @@ _ready:
MUTEX_UNLOCK(gil->switch_mutex);
#endif
- if (tstate_must_exit(tstate)) {
+ if (_PyThreadState_MustExit(tstate)) {
/* bpo-36475: If Py_Finalize() has been called and tstate is not
the thread which called Py_Finalize(), exit immediately the
thread.
diff --git a/Python/pystate.c b/Python/pystate.c
index 09c3538..b5c4fd7 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1907,6 +1907,10 @@ PyThreadState_Swap(PyThreadState *newts)
void
_PyThreadState_Bind(PyThreadState *tstate)
{
+ // gh-104690: If Python is being finalized and PyInterpreterState_Delete()
+ // was called, tstate becomes a dangling pointer.
+ assert(_PyThreadState_CheckConsistency(tstate));
+
bind_tstate(tstate);
// This makes sure there's a gilstate tstate bound
// as soon as possible.
@@ -2908,6 +2912,31 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate)
#endif
+// Check if a Python thread must exit immediately, rather than taking the GIL
+// if Py_Finalize() has been called.
+//
+// When this function is called by a daemon thread after Py_Finalize() has been
+// called, the GIL does no longer exist.
+//
+// tstate can be a dangling pointer (point to freed memory): only tstate value
+// is used, the pointer is not deferenced.
+//
+// tstate must be non-NULL.
+int
+_PyThreadState_MustExit(PyThreadState *tstate)
+{
+ /* bpo-39877: Access _PyRuntime directly rather than using
+ tstate->interp->runtime to support calls from Python daemon threads.
+ After Py_Finalize() has been called, tstate can be a dangling pointer:
+ point to PyThreadState freed memory. */
+ PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
+ if (finalizing == NULL) {
+ finalizing = _PyInterpreterState_GetFinalizing(tstate->interp);
+ }
+ return (finalizing != NULL && finalizing != tstate);
+}
+
+
#ifdef __cplusplus
}
#endif