diff --git a/src/utils.ts b/src/utils.ts index 6b1d216e7f..6ecca32d04 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; @@ -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() { diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index 36c4ef2efb..302b0109b6 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -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();