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

[bug] "retry_if_not_exception_type" does not behave the same in asynchronous and synchronous code #505

Open
deli-c1ous opened this issue Dec 18, 2024 · 0 comments

Comments

@deli-c1ous
Copy link

import logging
import asyncio

from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_not_exception_type, RetryError

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')


@retry(wait=wait_fixed(1), stop=stop_after_attempt(5))
async def fn1():
    logging.info('fn1')
    raise Exception


@retry(wait=wait_fixed(1), stop=stop_after_attempt(3), retry=retry_if_not_exception_type((RetryError, KeyboardInterrupt)))
async def fn2():
    logging.info('fn2')
    await fn1()


asyncio.run(fn2())

run it normally, the result is:

2024-12-18 14:04:37,067 - fn2
2024-12-18 14:04:37,067 - fn1
2024-12-18 14:04:38,080 - fn1
2024-12-18 14:04:39,084 - fn1
2024-12-18 14:04:40,086 - fn1
2024-12-18 14:04:41,102 - fn1
Exception...
RetryError...

press ctrl-c when runing halfway through, the result is:

2024-12-18 14:06:57,449 - fn2
2024-12-18 14:06:57,449 - fn1
2024-12-18 14:06:58,455 - fn1
(ctrl-c)
2024-12-18 14:07:00,017 - fn2
2024-12-18 14:07:00,017 - fn1
2024-12-18 14:07:01,023 - fn1
(ctrl-c)
2024-12-18 14:07:02,792 - fn2
2024-12-18 14:07:02,793 - fn1
2024-12-18 14:07:03,806 - fn1
(ctrl-c)
KeyboardInterrupt
RetryError

It didn't terminate at once when pressing ctrl-c, but retry instead. Seems like retry=retry_if_not_exception_type((RetryError, KeyboardInterrupt)) doesn't perform as expected.
In contrast, things in synchronous code goes well.

import logging

from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_not_exception_type, RetryError

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')


@retry(wait=wait_fixed(1), stop=stop_after_attempt(5))
def fn1():
    logging.info('fn1')
    raise Exception


@retry(wait=wait_fixed(1), stop=stop_after_attempt(3), retry=retry_if_not_exception_type((RetryError, KeyboardInterrupt)))
def fn2():
    logging.info('fn2')
    fn1()


fn2()
2024-12-18 14:35:50,932 - fn2
2024-12-18 14:35:50,932 - fn1
2024-12-18 14:35:51,933 - fn1
(ctrl-c)
KeyboardInterrupt...
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