bun
bun copied to clipboard
Bun.sleep() rounds incorrectly
What version of Bun is running?
1.0.26+c75e768a6
What platform is your computer?
Linux 6.1.31-gentoo-dist x86_64 13th Gen Intel(R) Core(TM) i7-1370P
What steps can reproduce the bug?
Run this code (based on a test from setTimeout.test.js):
while (true) {
let ten_ms = new Date();
ten_ms.setMilliseconds(ten_ms.getMilliseconds() + 12);
await Bun.sleep(ten_ms);
let now = new Date();
if (!(+ten_ms <= +now)) {
console.error({ten_ms: +ten_ms, now: +now});
break;
}
}
What is the expected behavior?
An infinite loop.
What do you see instead?
A break, since eventually Bun.sleep() will return a millisecond before the specified instant has been reached.
Additional information
The fix is simply adding:
millisecondsValue = JSC::jsNumber(ceil(millisecondsValue.asNumber()));
in BunObject.cpp:functionBunSleep. This will also make Bun.sleep(.001) sleep for a millisecond, which seems like safe behavior.
See #8833.