Skip to content

Commit a82228b

Browse files
committed
fix(NODE-3116): reschedule unreliable async interval first
1 parent d67eae0 commit a82228b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/utils.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,15 @@ export function makeInterruptibleAsyncInterval(
976976
// never completes, the `timeUntilNextCall` will continue to grow
977977
// negatively unbounded, so it will never trigger a reschedule here.
978978

979+
// This is possible in virtualized environments like AWS Lambda where our
980+
// clock is unreliable. In these cases the timer is "running" but never
981+
// actually completes, so we want to execute immediately and then attempt
982+
// to reschedule.
983+
if (timeUntilNextCall < 0) {
984+
executeAndReschedule();
985+
return;
986+
}
987+
979988
// debounce multiple calls to wake within the `minInterval`
980989
if (timeSinceLastWake < minInterval) {
981990
return;
@@ -986,14 +995,6 @@ export function makeInterruptibleAsyncInterval(
986995
if (timeUntilNextCall > minInterval) {
987996
reschedule(minInterval);
988997
}
989-
990-
// This is possible in virtualized environments like AWS Lambda where our
991-
// clock is unreliable. In these cases the timer is "running" but never
992-
// actually completes, so we want to execute immediately and then attempt
993-
// to reschedule.
994-
if (timeUntilNextCall < 0) {
995-
executeAndReschedule();
996-
}
997998
}
998999

9991000
function stop() {

test/unit/utils.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ describe('utils', function () {
140140

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

147150
return now();

0 commit comments

Comments
 (0)