Skip to content

Commit b7bf588

Browse files
Make state->initialized strictly monotonic.
1 parent 250606a commit b7bf588

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Modules/_testsinglephase.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ clear_state(module_state *state)
5050
Py_CLEAR(state->str_const);
5151
}
5252

53+
static int
54+
_set_initialized(_PyTime_t *initialized)
55+
{
56+
/* We go strictly monotonic to ensure each time is unique. */
57+
_PyTime_t prev;
58+
if (_PyTime_GetMonotonicClockWithInfo(&prev, NULL) != 0) {
59+
return -1;
60+
}
61+
/* We do a busy sleep since the interval should be super short. */
62+
_PyTime_t t;
63+
do {
64+
if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) != 0) {
65+
return -1;
66+
}
67+
} while (t == prev);
68+
69+
*initialized = t;
70+
return 0;
71+
}
72+
5373
static int
5474
init_state(module_state *state)
5575
{
@@ -58,9 +78,7 @@ init_state(module_state *state)
5878
state->int_const == NULL &&
5979
state->str_const == NULL);
6080

61-
state->initialized = _PyTime_GetMonotonicClock();
62-
if (state->initialized == 0) {
63-
PyErr_SetString(PyExc_RuntimeError, "could not get current time");
81+
if (_set_initialized(&state->initialized) != 0) {
6482
goto error;
6583
}
6684
assert(state->initialized > 0);

0 commit comments

Comments
 (0)