Skip to content

Commit 7ddd6e2

Browse files
White-Reaper0claude
andcommitted
gh-162421: Keep the full spin if the clock failed at the start
If PyTime_MonotonicRaw() failed when the wait started, spin_start is 0 and the first successful read made the spin look longer than MAX_SPIN_NS, ending it after one yield. Skip the new limits in that case so the spin stays as it was before them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 87a7a38 commit 7ddd6e2

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎Python/lock.c‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,22 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
120120
_Py_yield();
121121
spin_count++;
122122
#ifdef Py_GIL_DISABLED
123-
PyTime_t yielded;
124-
// silently ignore error: a clock of 0 leaves the spin unchanged
125-
(void)PyTime_MonotonicRaw(&yielded);
126-
if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) {
127-
spin_disabled_until = _PyTime_Add(yielded, SPIN_DISABLE_NS);
128-
spin_count = MAX_SPIN_COUNT;
123+
// If the clock failed when the wait started (spin_start is 0),
124+
// keep the full spin, as before these limits existed.
125+
if (spin_start != 0) {
126+
PyTime_t yielded;
127+
// silently ignore error: a clock of 0 shortens nothing
128+
(void)PyTime_MonotonicRaw(&yielded);
129+
if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) {
130+
spin_disabled_until = _PyTime_Add(yielded,
131+
SPIN_DISABLE_NS);
132+
spin_count = MAX_SPIN_COUNT;
133+
}
134+
else if (yielded - spin_start > MAX_SPIN_NS) {
135+
spin_count = MAX_SPIN_COUNT;
136+
}
137+
last_read = yielded;
129138
}
130-
else if (yielded - spin_start > MAX_SPIN_NS) {
131-
spin_count = MAX_SPIN_COUNT;
132-
}
133-
last_read = yielded;
134139
#endif
135140
continue;
136141
}

0 commit comments

Comments
 (0)