Skip to content

Commit d6d76d8

Browse files
authored
fix(cli): clamp setTimeout to 1ms (#5309)
CLI's custom executor can starve the event loop when doing a `setTimeout(fn, 0)` loop. This happens during tests like `test/built-ins/Atomics/waitAsync/bigint/true-for-timeout.js` Solution: clamp the delay to 1ms minimum (same as Node.js). Fixes #5308
1 parent 352ec3d commit d6d76d8

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

cli/src/executor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ impl JobExecutor for Executor {
204204
self.async_jobs
205205
.borrow_mut()
206206
.push_back(NativeAsyncJob::new(async move |context| {
207+
// Clamp timeout to prevent setTimeout(fn, 0) loops
208+
// from starving the main event loop. 1ms to match Node:
209+
// https://nodejs.org/api/timers.html#settimeoutcallback-delay-args
210+
const MIN_TIMEOUT: std::time::Duration =
211+
std::time::Duration::from_millis(1);
212+
let timeout = std::cmp::max(job.timeout().into(), MIN_TIMEOUT);
207213
let timer = async {
208-
smol::Timer::after(job.timeout().into()).await;
214+
smol::Timer::after(timeout).await;
209215
job.call(&mut context.borrow_mut())
210216
};
211217
let cancel = async {

0 commit comments

Comments
 (0)