Skip to content

Behavior change when reading max_tries and max_time from callables #160

Closed
@jvrsantacruz

Description

@jvrsantacruz

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 :)

-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:

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.

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