jd / jd/tenacity

[FEATURE REQUEST] warn when retried code is not idempotent

Open
#504 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
8.8k
Forks
359
Avg merge
1m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.