Skip to content

Unable to get retry working with unique constraint failure. #313

@neonquill

Description

@neonquill

I haven't been able to figure out how to get the retry parameter to work correctly with database errors. I can reproduce the test code with ZeroDivisionError exceptions, but not with database errors (such as a unique constraint violation).

Test code:

#!/usr/bin/env python3

from pony import orm

db = orm.Database()

class Foo(db.Entity):
    unique = orm.Required(int, unique=True)

@orm.db_session(retry=3)
def retry_unique():
    print("RETRY_UNIQUE")
    Foo(unique=1)

@orm.db_session(retry=3, retry_exceptions=[ZeroDivisionError])
def retry_zero_div():
    print("RETRY_ZERO_DIV")
    1/0

def main():
    db.bind(provider='postgres', user='postgres', password='password',
            host='127.0.0.1', database='test_retry')
    db.generate_mapping(create_tables=True)

    try:
        # This will run 4 times.
        retry_zero_div()
    except:
        pass

    try:
        # This will succeed (if db is empty).
        retry_unique()
        # This will only run once.  <- Bug?
        retry_unique()
    except:
        pass

if __name__ == '__main__':
    main()

Running this code on a clean database produces the following output:

RETRY_ZERO_DIV
RETRY_ZERO_DIV
RETRY_ZERO_DIV
RETRY_ZERO_DIV
RETRY_UNIQUE
RETRY_UNIQUE

I (correctly) see retry_zero_div() run 4 times (the first try plus the indicated 3 retries). For retry_unique() the first run succeeds (because there is no uniqueness violation) and then the second run throws an exception but is not re-tried (this seems wrong to me).

When I trace retry_zero_div() the retry gets caught in pony/pony/orm/core.py:462. However, when I run retry_unique() the code never hits that line. This makes me think that it's not an issue of having the wrong exceptions in retry_exceptions.

At this point, I'm not entirely sure if I'm doing something wrong or there is really a bug. Any advice would be appreciated.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions