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

Retry in recursive context is broken since 8.3.0 #478

Closed
sashaCher opened this issue Jun 24, 2024 · 3 comments · Fixed by #479
Closed

Retry in recursive context is broken since 8.3.0 #478

sashaCher opened this issue Jun 24, 2024 · 3 comments · Fixed by #479

Comments

@sashaCher
Copy link

Hello
We observed breaking change in @retry decorator behavior after upgrading the library from 8.2.3 to 8.3.0/8.4.1 version.

Assuming the next code sample (simplified and adjusted to illustrate the exact use-case):

from tenacity import RetryCallState, retry

MAX_RETRY_FIX_ATTEMPTS = 2


def do_retry(retry_state: RetryCallState):
    ex = retry_state.outcome.exception()
    _subject_: str = retry_state.args[0]

    if _subject_ == 'Fix':  # no retry on fix failure
        return False

    if retry_state.attempt_number >= MAX_RETRY_FIX_ATTEMPTS:
        return False

    if ex:
        do_fix_work()
        return True

    return False


@retry(reraise=True, retry=do_retry)
def _do_work(subject: str):
    if subject == 'Error':
        print(f'{subject} is not working')
        raise Exception(f'{subject} is not working')
    print(f'{subject} is working')


def do_any_work(subject: str):
    _do_work(subject)


def do_fix_work():
    _do_work('Fix')


if __name__ == '__main__':
    do_any_work('Error')

In version 8.2.3 it worked correctly means do_any_work function was called twice with do_fix_work in the middle and produced the next output:

Error is not working
Fix is working
Error is not working
Traceback (most recent call last):
...call-stack...
Exception: Error is not working

After upgrading to 8.3.0 or 8.4.1 the code is not working the same way.
There's no additional call of do_any_work after do_fix_work and the code does not end with error:

Error is not working
Fix is working

Process finished with exit code 0
@jd
Copy link
Owner

jd commented Jun 24, 2024

Thanks for reporting! I don't have time to dig into this now, but, cc @hasier in case that might be one of your recent PRs.

@hasier
Copy link
Contributor

hasier commented Jun 24, 2024

Just opened a fix in #479, looks like we were overwriting a local context when recursively calling a decorated function, sorry about that! @jd let me know if that looks ok to you :)

@mergify mergify bot closed this as completed in #479 Jun 24, 2024
@sashaCher
Copy link
Author

Thank you for fast resolution!!!

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

Successfully merging a pull request may close this issue.

3 participants