backoff icon indicating copy to clipboard operation
backoff copied to clipboard

Measure elapsed time after function call

Open lbenezriravin opened this issue 3 years ago • 2 comments

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.

lbenezriravin avatar Jul 21 '21 19:07 lbenezriravin

This would have addressed #186

fiendish avatar Dec 21 '22 21:12 fiendish

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

Kirusi avatar Jan 31 '23 15:01 Kirusi