Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] warn when retried code is not idempotent #504

Open
Clement-Lelievre opened this issue Dec 17, 2024 · 0 comments
Open

[FEATURE REQUEST] warn when retried code is not idempotent #504

Clement-Lelievre opened this issue Dec 17, 2024 · 0 comments

Comments

@Clement-Lelievre
Copy link

Clement-Lelievre commented Dec 17, 2024

Hi,

in advance: I've looked at the past issues and PRs in this repo, looking for terms like "stateful" and "idempotent" and did not see anything related to my issue. Sorry if I missed it. Also I've had a quick glance at the doc but don't know it inside and out.

The below request might be out of scope for tenacity, because, as will be seen below, the issue is due to a bad pattern in the code I was looking at.
But I'll still share what happened to me today, and maybe that can give rise to a nice feature for tenacity.
I'll start with the result: tedious debugging because the traceback was not showing the actual, relevant issue.

The reason is that the retried code was not idempotent, specifically the decorated method deleted a class attribute, which can work without error at most once, while the nb of retries was 3.

Below is a MRE:

# python==3.12.7, tenacity==9.0.0
from tenacity import stop_after_attempt, retry

class C:
    def __init__(self):
        self.a = ""
        
    @retry(stop=stop_after_attempt(3))
    def f(self):
        del self.a # represents a non idempotent action
        1/0  # represents the actual prod error

C().f()  # AttributeError: 'C' object has no attribute 'a'

In a Machine Learning context, del is quite common to clean up memory and so this is a real use case.
One way to avoid this is to decorate the minimal necessary code and avoid the above pattern, or use features like retry_if_exception_type but I'm wondering if it would be possible for tenacity to issue a warning log whenever there are > 1 error types encountered across all the retries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant