mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
gh-97922: Run the GC only on eval breaker (#97920)
This commit is contained in:
parent
c66dbddfba
commit
83eb827247
8 changed files with 74 additions and 14 deletions
|
@ -5,6 +5,7 @@
|
|||
#include "pycore_pyerrors.h" // _PyErr_Fetch()
|
||||
#include "pycore_pylifecycle.h" // _PyErr_Print()
|
||||
#include "pycore_initconfig.h" // _PyStatus_OK()
|
||||
#include "pycore_interp.h" // _Py_RunGC()
|
||||
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
|
||||
|
||||
/*
|
||||
|
@ -69,7 +70,8 @@ COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
|
|||
&& _Py_ThreadCanHandleSignals(interp))
|
||||
| (_Py_atomic_load_relaxed_int32(&ceval2->pending.calls_to_do)
|
||||
&& _Py_ThreadCanHandlePendingCalls())
|
||||
| ceval2->pending.async_exc);
|
||||
| ceval2->pending.async_exc
|
||||
| _Py_atomic_load_relaxed_int32(&ceval2->gc_scheduled));
|
||||
}
|
||||
|
||||
|
||||
|
@ -938,6 +940,7 @@ _Py_HandlePending(PyThreadState *tstate)
|
|||
{
|
||||
_PyRuntimeState * const runtime = &_PyRuntime;
|
||||
struct _ceval_runtime_state *ceval = &runtime->ceval;
|
||||
struct _ceval_state *interp_ceval_state = &tstate->interp->ceval;
|
||||
|
||||
/* Pending signals */
|
||||
if (_Py_atomic_load_relaxed_int32(&ceval->signals_pending)) {
|
||||
|
@ -947,20 +950,26 @@ _Py_HandlePending(PyThreadState *tstate)
|
|||
}
|
||||
|
||||
/* Pending calls */
|
||||
struct _ceval_state *ceval2 = &tstate->interp->ceval;
|
||||
if (_Py_atomic_load_relaxed_int32(&ceval2->pending.calls_to_do)) {
|
||||
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->pending.calls_to_do)) {
|
||||
if (make_pending_calls(tstate->interp) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* GC scheduled to run */
|
||||
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->gc_scheduled)) {
|
||||
_Py_atomic_store_relaxed(&interp_ceval_state->gc_scheduled, 0);
|
||||
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, interp_ceval_state);
|
||||
_Py_RunGC(tstate);
|
||||
}
|
||||
|
||||
/* GIL drop request */
|
||||
if (_Py_atomic_load_relaxed_int32(&ceval2->gil_drop_request)) {
|
||||
if (_Py_atomic_load_relaxed_int32(&interp_ceval_state->gil_drop_request)) {
|
||||
/* Give another thread a chance */
|
||||
if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
|
||||
Py_FatalError("tstate mix-up");
|
||||
}
|
||||
drop_gil(ceval, ceval2, tstate);
|
||||
drop_gil(ceval, interp_ceval_state, tstate);
|
||||
|
||||
/* Other threads may run now */
|
||||
|
||||
|
@ -981,16 +990,17 @@ _Py_HandlePending(PyThreadState *tstate)
|
|||
return -1;
|
||||
}
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
// bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
|
||||
// different thread than the Python thread, in which case
|
||||
|
||||
// It is possible that some of the conditions that trigger the eval breaker
|
||||
// are called in a different thread than the Python thread. An example of
|
||||
// this is bpo-42296: On Windows, _PyEval_SignalReceived() can be called in
|
||||
// a different thread than the Python thread, in which case
|
||||
// _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
|
||||
// current Python thread with the correct _Py_ThreadCanHandleSignals()
|
||||
// value. It prevents to interrupt the eval loop at every instruction if
|
||||
// the current Python thread cannot handle signals (if
|
||||
// _Py_ThreadCanHandleSignals() is false).
|
||||
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
|
||||
#endif
|
||||
COMPUTE_EVAL_BREAKER(tstate->interp, ceval, interp_ceval_state);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue