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.
Fixes #3628
🛠 Fix timer drift in
os.sleep
andpullSignal
due to floating-point precision errors📌 Summary
This PR addresses a long-standing issue in OpenComputers for MC 1.7.10 and 1.12.2 where
os.sleep(timeout)
andevent.pull(timeout)
exhibit systematic and predictable drift due to floating-point precision limitations. Over time, this drift accumulates, leading to sleep durations that exceed the intended interval by one or more ticks.🧩 Problem Description
os.sleep(timeout)
internally computes a deadline usingcomputer.uptime() + timeout
.0.05
) in binary, the computed deadline may slightly exceed the intended value.event.pull(deadline - computer.uptime())
is called, the result may fall just beyond the current tick boundary, causing the coroutine to yield for an extra tick.computer.uptime()
increases, leading to cumulative drift.pullSignal(timeout)
and other sleep-like constructs.✅ Fix Overview
1e-6
) to account for floating-point error when comparing current time to deadline.event.pull(deadline - computer.uptime())
calls with a guarded version that avoids yielding if the remaining time is below epsilon.0.001
) to prevent tight polling loops and CPU saturation.os.sleep
,pullSignal
, and internal sleep routines.🔬 Validation & Testing
A Lua-based precision test script was used to measure drift over 100–1000 consecutive
os.sleep(0.05)
calls: