Skip to content

Commit 15c4b6d

Browse files
committed
gh-151518: Strengthen the STW attach fairness regression
Hold each test pause for 10 ms and repeat pauses in C to reduce the opportunities for a waiting thread to run between pause requests. Clarify that parking rechecks the thread state before sleeping.
1 parent c34abd4 commit 15c4b6d

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎Modules/_testinternalcapi.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pycore_instruction_sequence.h" // _PyInstructionSequence_New()
3131
#include "pycore_interpframe.h" // _PyFrame_GetFunction()
3232
#include "pycore_jit.h" // _PyJIT_AddressInJitCode()
33+
#include "pycore_lock.h" // PyEvent_WaitTimed()
3334
#include "pycore_object.h" // _PyObject_IsFreed()
3435
#include "pycore_optimizer.h" // _Py_Executor_DependsOn
3536
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
@@ -212,8 +213,14 @@ static PyObject *
212213
test_stop_the_world(PyObject *self, PyObject *Py_UNUSED(args))
213214
{
214215
PyInterpreterState *interp = _PyInterpreterState_GET();
215-
_PyEval_StopTheWorld(interp);
216-
_PyEval_StartTheWorld(interp);
216+
// Request consecutive pauses without running Python code between them.
217+
for (int i = 0; i < 100; i++) {
218+
_PyEval_StopTheWorld(interp);
219+
// Give detached threads time to try to reattach during the pause.
220+
PyEvent event = {0};
221+
PyEvent_WaitTimed(&event, 10 * 1000 * 1000, /*detach=*/0);
222+
_PyEval_StartTheWorld(interp);
223+
}
217224
Py_RETURN_NONE;
218225
}
219226

‎Python/pystate.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ tstate_wait_attach(PyThreadState *tstate)
22692269
state = _Py_THREAD_SUSPENDED_WAITING;
22702270
}
22712271
if (state == _Py_THREAD_SUSPENDED_WAITING) {
2272-
// Wait until the stop-the-world thread lets us attach.
2272+
// Park rechecks the state before sleeping, in case we were resumed.
22732273
_PyParkingLot_Park(&tstate->state, &state, sizeof(tstate->state),
22742274
/*timeout=*/-1, NULL, /*detach=*/0);
22752275
}

0 commit comments

Comments
 (0)