Skip to content

maxRetryTime is only checked when an attempt fails, so attempts can start after it #102

Open

Description

@ritabratamaiti

The README describes maxRetryTime as "The maximum time (in milliseconds) that the retried operation is allowed to run" (README.md#L110). RetryOperation.prototype.retry only compares the elapsed time with maxRetryTime when an attempt fails (retry_operation.js#L52). If it's still under the limit, it schedules the whole next timeout (#L60-L86), even when that timeout ends well past maxRetryTime. So a new attempt can start after the limit. The overshoot can be almost as long as the wait that was scheduled, so it's bounded by the largest timeout (at most maxTimeout).

Environment: retry 0.13.1 (latest on npm, same code as master at 11efd6e), Node 18.19.1, Ubuntu 24.04 x86_64.

Reproduction

const retry = require('retry');
const options = {retries: 5, factor: 2, minTimeout: 100, maxRetryTime: 1000};
console.log(retry.timeouts(options)); // [100, 200, 400, 800, 1600]

const op = retry.operation(options);
let t0;
op.attempt(() => {
  t0 ??= Date.now();
  console.log('attempt at', Date.now() - t0, 'ms');
  op.retry(new Error('fail'));
});
attempt at 0 ms
attempt at 102 ms
attempt at 303 ms
attempt at 704 ms
attempt at 1505 ms

Expected

No attempt starts after maxRetryTime has passed.

What happens instead

The failure at 704 ms is under the 1000 ms limit, so the 800 ms timeout gets scheduled and the next attempt starts at about 1505 ms, 505 ms after the limit. The gap grows with the timeouts. With {minTimeout: 1000, factor: 2, maxRetryTime: 60000} and attempts that each take 2 s to fail, the attempts start at 0, 3, 7, 13, 23, 41 and 75 s. The one at 41 s fails at 43 s, under the limit, so the 32 s timeout is scheduled and the last attempt starts 15 s after the limit.

Possible fix

When scheduling, cap the timeout at the time that's left. The existing check already stops when nothing is left.

var remaining = this._maxRetryTime - (currentTime - this._operationStart);
timeout = Math.min(timeout, remaining);

With this change the last attempt in the first example starts at 1000 ms (1001 ms with timer delay), and the tests in test/integration still pass. p-retry 9.0.0 does the same thing (delayForRetry(Math.min(effectiveDelay, remainingTimeAfterShouldRetry), options)). If the current behavior is intended, the README could say that maxRetryTime is checked only when an attempt fails and that the last wait can run past it.

Either change is small, and I can send it as a PR.

Notes

It happens whenever an attempt fails before the limit and the time that's left is shorter than the next timeout. So when maxRetryTime is what ends the operation, the last attempt starts late unless the limit happens to pass while an attempt is running. forever: true behaves the same way.

I searched open and closed issues and PRs for maxRetryTime and didn't find this. #92 asks for a per-attempt timeout, which is a different thing. The open PR #100 (nextTimeout()) touches the same part of retry(); with the cap placed before this._nextTimeout = timeout, nextTimeout() would report the shortened wait.


Checking retry start times with Haze

I modeled the elapsed-time check and timeout scheduling in Haze. The complete model uses the reproduction's five timeouts and 1,000 ms limit. Its capped parameter switches between the released scheduling rule and the proposed cap:

let wait := if capped then min(timeout(used), max_retry_time - clock) else timeout(used)
clock := clock + wait
used := used + 1
last_start := clock

The property is that no attempt starts after the limit. These are excerpts from the checks for immediately failing attempts:

theorem starts_within_limit : Operation(false, false) |= always[12] last_start <= max_retry_time
theorem starts_within_limit_capped : Operation(true, false) |= always[12] last_start <= max_retry_time
Scheduling rule Result Last attempt's start time
Released code Rejected 1500 ms
Capped wait Accepted 1000 ms

The rejected state has clock = 1500, used = 4 and last_start = 1500: the fifth attempt starts after the 1,000 ms limit. The file also checks attempts lasting 0, 50, 100 or 150 ms; the uncapped rule fails and the capped rule passes there too.

The model's clock accounts for requested waits and those attempt durations. It does not model OS timer delays. The package run above confirms the scheduling overshoot.

Run the Haze model and compare its output

The model, checker and recorded output are pinned to Haze commit 452a5993. With elan installed, these commands build and run that revision in a Unix shell:

git clone https://github.com/ritabratamaiti/haze-lang
cd haze-lang
git checkout --detach 452a5993a68962653afd3eb5382e9c4abcfcd421
lake build haze
.lake/build/bin/haze examples/Found/node_retry.hz

Exit status 1 is expected: the file deliberately includes rejected claims. The comparison variants and full diagnostics are in the same file.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions