Skip to content

Commit 7daa990

Browse files
fix(core): enforce maxElapsed against wall-clock in ExponentialBackoff.execute
Closes #3726
1 parent a7c734c commit 7daa990

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/core": patch
3+
---
4+
5+
Honor `maxElapsed` against real wall-clock time in `ExponentialBackoff.execute()`, including time spent inside the callback—not only sleep delays.

‎packages/core/src/v3/apps/backoff.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,27 @@ export class ExponentialBackoff {
290290
> {
291291
let elapsedMs = 0;
292292
let finalError: unknown = undefined;
293+
// maxElapsed is in seconds (see class docs). Enforce wall-clock time for
294+
// the whole execute() loop (sleeps + callbacks), not only sleep delays.
295+
const maxElapsedMs = this.#maxElapsed * 1000;
296+
const wallStart = Date.now();
293297

294298
for await (const { delay, retry } of this) {
299+
if (Date.now() - wallStart > maxElapsedMs) {
300+
break;
301+
}
302+
295303
const start = Date.now();
296304

297305
if (retry > 0) {
298306
console.log(`Retrying in ${delay.milliseconds}ms`);
299307
await timeout(delay.milliseconds);
300308
}
301309

310+
if (Date.now() - wallStart > maxElapsedMs) {
311+
break;
312+
}
313+
302314
let attemptTimeout: NodeJS.Timeout | undefined = undefined;
303315

304316
try {

0 commit comments

Comments
 (0)