Skip to content

Commit

Permalink
fix(NODE-3116): reschedule unreliable async interval first
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 4, 2021
1 parent d67eae0 commit a82228b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,15 @@ export function makeInterruptibleAsyncInterval(
// never completes, the `timeUntilNextCall` will continue to grow
// negatively unbounded, so it will never trigger a reschedule here.

// This is possible in virtualized environments like AWS Lambda where our
// clock is unreliable. In these cases the timer is "running" but never
// actually completes, so we want to execute immediately and then attempt
// to reschedule.
if (timeUntilNextCall < 0) {
executeAndReschedule();
return;
}

// debounce multiple calls to wake within the `minInterval`
if (timeSinceLastWake < minInterval) {
return;
Expand All @@ -986,14 +995,6 @@ export function makeInterruptibleAsyncInterval(
if (timeUntilNextCall > minInterval) {
reschedule(minInterval);
}

// This is possible in virtualized environments like AWS Lambda where our
// clock is unreliable. In these cases the timer is "running" but never
// actually completes, so we want to execute immediately and then attempt
// to reschedule.
if (timeUntilNextCall < 0) {
executeAndReschedule();
}
}

function stop() {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ describe('utils', function () {

// needs to happen on the third call because `wake` checks
// the `currentTime` at the beginning of the function
// The value of now() is not actually negative in the case of
// the unreliable check so we force to a negative value now
// for this test.
if (clockCalled === 3) {
return now() - 100000;
return -1;
}

return now();
Expand Down

0 comments on commit a82228b

Please sign in to comment.