Skip to content

Commit 26baa74

Browse files
gh-104341: Adjust tstate_must_exit() to Respect Interpreter Finalization (gh-104437)
With the move to a per-interpreter GIL, this check slipped through the cracks.
1 parent cb88ae6 commit 26baa74

File tree

11 files changed

+56
-10
lines changed

11 files changed

+56
-10
lines changed

Include/cpython/pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ PyAPI_FUNC(const char *) _Py_gitidentifier(void);
5252
PyAPI_FUNC(const char *) _Py_gitversion(void);
5353

5454
PyAPI_FUNC(int) _Py_IsFinalizing(void);
55+
PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp);
5556

5657
/* Random */
5758
PyAPI_FUNC(int) _PyOS_URandom(void *buffer, Py_ssize_t size);

Include/internal/pycore_interp.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ struct _is {
8383
int _initialized;
8484
int finalizing;
8585

86+
/* Set by Py_EndInterpreter().
87+
88+
Use _PyInterpreterState_GetFinalizing()
89+
and _PyInterpreterState_SetFinalizing()
90+
to access it, don't access it directly. */
91+
_Py_atomic_address _finalizing;
92+
8693
struct _obmalloc_state obmalloc;
8794

8895
struct _ceval_state ceval;
@@ -191,6 +198,17 @@ struct _is {
191198
extern void _PyInterpreterState_Clear(PyThreadState *tstate);
192199

193200

201+
static inline PyThreadState*
202+
_PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
203+
return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);
204+
}
205+
206+
static inline void
207+
_PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
208+
_Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);
209+
}
210+
211+
194212
/* cross-interpreter data registry */
195213

196214
/* For now we use a global registry of shareable classes. An

Modules/_asynciomodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ future_init(FutureObj *fut, PyObject *loop)
526526
if (is_true < 0) {
527527
return -1;
528528
}
529-
if (is_true && !_Py_IsFinalizing()) {
529+
if (is_true && !_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
530530
/* Only try to capture the traceback if the interpreter is not being
531531
finalized. The original motivation to add a `_Py_IsFinalizing()`
532532
call was to prevent SIGSEGV when a Future is created in a __del__

Modules/_io/bufferedio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ _enter_buffered_busy(buffered *self)
293293
"reentrant call inside %R", self);
294294
return 0;
295295
}
296-
relax_locking = _Py_IsFinalizing();
296+
PyInterpreterState *interp = PyInterpreterState_Get();
297+
relax_locking = _Py_IsInterpreterFinalizing(interp);
297298
Py_BEGIN_ALLOW_THREADS
298299
if (!relax_locking)
299300
st = PyThread_acquire_lock(self->lock, 1);

Modules/_sqlite/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ connection_close(pysqlite_Connection *self)
419419
{
420420
/* If close is implicitly called as a result of interpreter
421421
* tear-down, we must not call back into Python. */
422-
if (_Py_IsFinalizing()) {
422+
if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
423423
remove_callbacks(self->db);
424424
}
425425
(void)connection_exec_stmt(self, "ROLLBACK");

Modules/_winapi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ overlapped_dealloc(OverlappedObject *self)
133133
{
134134
/* The operation is no longer pending -- nothing to do. */
135135
}
136-
else if (_Py_IsFinalizing())
136+
else if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get()))
137137
{
138138
/* The operation is still pending -- give a warning. This
139139
will probably only happen on Windows XP. */

Python/_warnings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ get_warnings_attr(PyInterpreterState *interp, PyObject *attr, int try_import)
198198
PyObject *warnings_module, *obj;
199199

200200
/* don't try to import after the start of the Python finallization */
201-
if (try_import && !_Py_IsFinalizing()) {
201+
if (try_import && !_Py_IsInterpreterFinalizing(interp)) {
202202
warnings_module = PyImport_Import(&_Py_ID(warnings));
203203
if (warnings_module == NULL) {
204204
/* Fallback to the C implementation if we cannot get

Python/ceval_gil.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ tstate_must_exit(PyThreadState *tstate)
332332
After Py_Finalize() has been called, tstate can be a dangling pointer:
333333
point to PyThreadState freed memory. */
334334
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
335+
if (finalizing == NULL) {
336+
finalizing = _PyInterpreterState_GetFinalizing(tstate->interp);
337+
}
335338
return (finalizing != NULL && finalizing != tstate);
336339
}
337340

Python/pylifecycle.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,7 @@ Py_FinalizeEx(void)
17881788

17891789
/* Remaining daemon threads will automatically exit
17901790
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
1791+
_PyInterpreterState_SetFinalizing(tstate->interp, tstate);
17911792
_PyRuntimeState_SetFinalizing(runtime, tstate);
17921793
runtime->initialized = 0;
17931794
runtime->core_initialized = 0;
@@ -2142,6 +2143,10 @@ Py_EndInterpreter(PyThreadState *tstate)
21422143
Py_FatalError("not the last thread");
21432144
}
21442145

2146+
/* Remaining daemon threads will automatically exit
2147+
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
2148+
_PyInterpreterState_SetFinalizing(interp, tstate);
2149+
21452150
// XXX Call something like _PyImport_Disable() here?
21462151

21472152
_PyImport_FiniExternal(tstate->interp);
@@ -2152,6 +2157,18 @@ Py_EndInterpreter(PyThreadState *tstate)
21522157
finalize_interp_delete(tstate->interp);
21532158
}
21542159

2160+
int
2161+
_Py_IsInterpreterFinalizing(PyInterpreterState *interp)
2162+
{
2163+
/* We check the runtime first since, in a daemon thread,
2164+
interp might be dangling pointer. */
2165+
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
2166+
if (finalizing == NULL) {
2167+
finalizing = _PyInterpreterState_GetFinalizing(interp);
2168+
}
2169+
return finalizing != NULL;
2170+
}
2171+
21552172
/* Add the __main__ module */
21562173

21572174
static PyStatus

Python/pystate.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,11 +1436,13 @@ PyThreadState_Clear(PyThreadState *tstate)
14361436

14371437
if (verbose && tstate->cframe->current_frame != NULL) {
14381438
/* bpo-20526: After the main thread calls
1439-
_PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
1440-
exit when trying to take the GIL. If a thread exit in the middle of
1441-
_PyEval_EvalFrameDefault(), tstate->frame is not reset to its
1442-
previous value. It is more likely with daemon threads, but it can
1443-
happen with regular threads if threading._shutdown() fails
1439+
_PyInterpreterState_SetFinalizing() in Py_FinalizeEx()
1440+
(or in Py_EndInterpreter() for subinterpreters),
1441+
threads must exit when trying to take the GIL.
1442+
If a thread exit in the middle of _PyEval_EvalFrameDefault(),
1443+
tstate->frame is not reset to its previous value.
1444+
It is more likely with daemon threads, but it can happen
1445+
with regular threads if threading._shutdown() fails
14441446
(ex: interrupted by CTRL+C). */
14451447
fprintf(stderr,
14461448
"PyThreadState_Clear: warning: thread still has a frame\n");

Python/sysmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ _PySys_ClearAuditHooks(PyThreadState *ts)
332332
}
333333

334334
_PyRuntimeState *runtime = ts->interp->runtime;
335+
/* The hooks are global so we have to check for runtime finalization. */
335336
PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
336337
assert(finalizing == ts);
337338
if (finalizing != ts) {
@@ -2039,6 +2040,9 @@ sys__clear_type_cache_impl(PyObject *module)
20392040
Py_RETURN_NONE;
20402041
}
20412042

2043+
/* Note that, for now, we do not have a per-interpreter equivalent
2044+
for sys.is_finalizing(). */
2045+
20422046
/*[clinic input]
20432047
sys.is_finalizing
20442048

0 commit comments

Comments
 (0)