Repository navigation
Make Thread/sleep sleep for what it is asked - #1340
Merged
Merged
Conversation
yogthos
force-pushed
the
fix/sleep-overshoot
branch
2 times, most recently
from
October 14, 2026 01:39
4565ac9 to
85ed91a
Compare
A thread's timed wait counted its relative timeout from the truncated millisecond, so it overran the deadline by up to 1ms, and since 45bfe3d rounded deadlines up as well (Thread/sleep 1) took ~2.5ms. (Thread/sleep 0) waited for the next clock tick, ~1.25ms, and (Thread/sleep ms nanos) dropped the nanos, so (Thread/sleep 0 100000) was the same 1.25ms where 0.8.20 returned at once. ebb's sample stress test leaned on that timing and started timing out on CI. Timeouts are now measured from the precise monotonic clock, and a sleep's deadline is an exact rational millisecond compared at full precision (whole deadlines compare exactly as before). sleep(0) checks the interrupt flag and yields, as the JDK does. TimeUnit.sleep passes sub-millisecond amounts through instead of truncating them to zero.
Thread/sleep took a negative millis or out-of-range nanos as a zero sleep where the JDK throws IllegalArgumentException. TimeUnit.sleep(0) went through the new zero-length path and threw on an interrupted thread; the JDK's guard skips the sleep entirely for a timeout <= 0.
10b failed on the Android runner with an absolute 0.5ms ceiling on a yield. Each row now compares two things measured interleaved, so load stretches both sides.
mono-deadline rounds now up so a wait is never early, which also made a zero timeout wait for the next millisecond tick: (.poll q 0 ms) and (deref p 0 nil) cost ~1.25ms a call.
yogthos
force-pushed
the
fix/sleep-overshoot
branch
from
October 14, 2026 03:02
85ed91a to
6a5b562
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since 0.8.21 (45bfe3d, rounding deadlines up) Thread/sleep overshoots, and sleep(0) / sleep(0, n) wait for a clock tick instead of returning. Measured on an M1 Max, 200 calls each:
45bfe3d^ vs 45bfe3d confirms the change: 0.000 ms vs 1.254 ms for sleep(0, 100000). It showed up as an intermittent timeout in ebb's sample stress test, whose notifier and reads both use sub-millisecond sleeps (jlt-commons/ebb#14 has the ebb side; the root of that hang is in ebb).
Three causes:
Section 10 of the asynctimer gate covers it: the timeout handed to condition-wait never exceeds the time left (failed before), sleep(0) does not wait for a tick (failed before), sleep(0, 100000) is at least 0.1ms and well under 1ms. make test passes locally.
Merging this before jlt-commons/ebb#14 would turn ebb CI red every run: with real 100us/500us sleeps the stress test's notifier outruns its reads 5x and the unfixed sample never finishes.