Skip to content

Commit

Permalink
set minimum timeout to be 4 milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 15, 2021
1 parent 49ec3d1 commit a607925
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/tests/unit/timers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ unitTest(async function timerMaxCpuBug(): Promise<void> {
const { opsDispatched } = Deno.metrics();
await waitForMs(100);
const opsDispatched_ = Deno.metrics().opsDispatched;
console.log("x", opsDispatched_ - opsDispatched);
assert(opsDispatched_ - opsDispatched < 10);
});

Expand Down
6 changes: 6 additions & 0 deletions extensions/timers/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ pub fn op_global_timer_start(
timeout: u64,
_: (),
) -> Result<(), AnyError> {
// According to spec, minimum allowed timeout is 4 ms.
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers
// TODO(#10974) Per spec this is actually a little more complicated than this.
// The minimum timeout depends on the nesting level of the timeout.
let timeout = std::cmp::max(timeout, 4);

let deadline = Instant::now() + Duration::from_millis(timeout);
let global_timer = state.borrow_mut::<GlobalTimer>();
global_timer.new_timeout(deadline);
Expand Down

0 comments on commit a607925

Please sign in to comment.