Repository: litl/backoff

Issue:
Behavior change when reading max_tries and max_time from callables
Hi there!

Using a callable to read `max_tries` and `max_time` was really useful to query an application config object.
Every time the function was called, the value would be read again and so it remained configurable. 

After updating to version 2+, we realized that the `max_tries` and `max_time` are read from the callable once and then cached for every other usage of the function. We saw this when some unit tests that were testing that the application reloads the config correctly were broken.

This was really an use case for us and we're pinned to the previous version we had as we cannot recreate every other object that has a decorated function on every config reload and so we'd consider this a regression on this nice functionality :)

```diff
-backoff==1.10.0
+backoff==2.0.1
```

This minimal example reproduces the problem, the output shows the difference when running in those two versions:

```python
import backoff


CONFIG = {'max_tries': 1}


def _from_config():
    print('Reading max_tries: ', CONFIG)
    return CONFIG['max_tries']


@backoff.on_exception(
    backoff.expo,
    Exception,
    max_tries=_from_config,
    on_giveup=lambda details: print('Total tries', details['tries']),
)
def function():
    print('Calling function')
    raise Exception(f'boom!')


print('First attempt', CONFIG)
try:
    function()
except:
    pass

print('----')
CONFIG['max_tries'] = 2  # Config changed

print('Second attempt', CONFIG)
try:
    function()
except:
    pass
```

The output for `1.10.0` shows that the configuration is read again on every function call:

```
First attempt {'max_tries': 1}
Reading max_tries:  {'max_tries': 1}
Calling function
Total tries 1
----
Second attempt {'max_tries': 2}
Reading max_tries:  {'max_tries': 2}
Calling function
Calling function
Total tries 2
```

While on `2.0.1` this is only made once and then cached for every other execution:

```
First attempt {'max_tries': 1}
Reading max_tries:  {'max_tries': 1}
Calling function
Total tries 1
----
Second attempt {'max_tries': 2}
Calling function
Total tries 1
```

As you can see, the second time the function already cached the `max_tries` and does not read it again. 


Comments:

Patch:
diff --git a/backoff/_sync.py b/backoff/_sync.py
index ecc592d..1c54ecc 100644
--- a/backoff/_sync.py
+++ b/backoff/_sync.py
@@ -28,11 +28,8 @@ def retry_predicate(target, wait_gen, predicate,
 
     @functools.wraps(target)
     def retry(*args, **kwargs):
-
-        # update variables from outer function args
-        nonlocal max_tries, max_time
-        max_tries = _maybe_call(max_tries)
-        max_time = _maybe_call(max_time)
+        max_tries_value = _maybe_call(max_tries)
+        max_time_value = _maybe_call(max_time)
 
         tries = 0
         start = datetime.datetime.now()
@@ -50,9 +47,9 @@ def retry_predicate(target, wait_gen, predicate,
 
             ret = target(*args, **kwargs)
             if predicate(ret):
-                max_tries_exceeded = (tries == max_tries)
+                max_tries_exceeded = (tries == max_tries_value)
                 max_time_exceeded = (max_time is not None and
-                                     elapsed >= max_time)
+                                     elapsed >= max_time_value)
 
                 if max_tries_exceeded or max_time_exceeded:
                     _call_handlers(on_giveup, **details, value=ret)
@@ -86,11 +83,8 @@ def retry_exception(target, wait_gen, exception,
 
     @functools.wraps(target)
     def retry(*args, **kwargs):
-
-        # update variables from outer function args
-        nonlocal max_tries, max_time
-        max_tries = _maybe_call(max_tries)
-        max_time = _maybe_call(max_time)
+        max_tries_value = _maybe_call(max_tries)
+        max_time_value = _maybe_call(max_time)
 
         tries = 0
         start = datetime.datetime.now()
@@ -109,9 +103,9 @@ def retry_exception(target, wait_gen, exception,
             try:
                 ret = target(*args, **kwargs)
             except exception as e:
-                max_tries_exceeded = (tries == max_tries)
+                max_tries_exceeded = (tries == max_tries_value)
                 max_time_exceeded = (max_time is not None and
-                                     elapsed >= max_time)
+                                     elapsed >= max_time_value)
 
                 if giveup(e) or max_tries_exceeded or max_time_exceeded:
                     _call_handlers(on_giveup, **details)


Test patch:
diff --git a/tests/test_backoff.py b/tests/test_backoff.py
index e6b3657..4e70fec 100644
--- a/tests/test_backoff.py
+++ b/tests/test_backoff.py
@@ -548,6 +548,29 @@ def test_on_exception_callable_max_tries(monkeypatch):
     assert len(log) == 3
 
 
+def test_on_exception_callable_max_tries_reads_every_time(monkeypatch):
+    monkeypatch.setattr('time.sleep', lambda x: None)
+
+    lookups = []
+    def lookup_max_tries():
+        lookups.append(True)
+        return 3
+
+    @backoff.on_exception(backoff.constant,
+                          ValueError,
+                          max_tries=lookup_max_tries)
+    def exceptor():
+        raise ValueError()
+
+    with pytest.raises(ValueError):
+        exceptor()
+
+    with pytest.raises(ValueError):
+        exceptor()
+
+    assert len(lookups) == 2
+
+
 def test_on_exception_callable_gen_kwargs():
 
     def lookup_foo():
diff --git a/tests/test_backoff_async.py b/tests/test_backoff_async.py
index d8219fd..341a6b8 100644
--- a/tests/test_backoff_async.py
+++ b/tests/test_backoff_async.py
@@ -571,6 +571,31 @@ async def test_on_exception_callable_max_tries(monkeypatch):
     assert len(log) == 3
 
 
+@pytest.mark.asyncio
+async def test_on_exception_callable_max_tries_reads_every_time(monkeypatch):
+    monkeypatch.setattr('asyncio.sleep', _await_none)
+
+    lookups = []
+    def lookup_max_tries():
+        lookups.append(True)
+        return 3
+
+    @backoff.on_exception(backoff.constant,
+                          ValueError,
+                          max_tries=lookup_max_tries)
+    def exceptor():
+        raise ValueError()
+
+    with pytest.raises(ValueError):
+        exceptor()
+
+    with pytest.raises(ValueError):
+        exceptor()
+
+    assert len(lookups) == 2
+
+
+
 @pytest.mark.asyncio
 async def test_on_exception_callable_gen_kwargs():