Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.

Conversation

@lihu-zhong
Copy link

This patch fixes what I consider to be unexpected behavior:

Steps to reproduce

import time

import backoff


@backoff.on_exception(backoff.constant, RuntimeError, jitter=None, max_time=1)
def foo():
    print("Running")
    time.sleep(2)
    raise


def real_time():
    start = time.time()
    try:
        foo()
    except:
        pass
    print(time.time() - start)

real_time()

Observed behavior

$ poetry run python foo.py 
Running
Running
5.004697322845459

Expected behavior
When I specify the max_time argument, I expect the worst-case upper bound for execution time to be the specified timeout + the execution time of a single function call. Moreover, I would not expect retries to trigger if the give up condition is already fulfilled after the first function execution.

This behavior appears to be caused by collected elapsed for the details before the target function is executed. This patch should fix both of the above bugs by swapping that ordering.

Lihu Ben-Ezri-Ravin added 2 commits July 21, 2021 15:12
When I specify the `max_time` argument, I expect the worst-case upper
bound for execution time to be the specified timeout + the execution
time of a single function call. Moreover, I would not expect retries to
trigger if the give up condition is already fulfilled after the first
function execution.

The existing behavior appears to be caused by collected `elapsed` for
the `details` before the target function is executed. Both of the above
bugs should be fixed by swapping that ordering.
@fiendish
Copy link

This would have addressed #186

@Kirusi
Copy link

Kirusi commented Jan 31, 2023

I forked the library as https://github.com/Kirusi/improved_backoff. If you depend on properly working max_time feel free to use it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants