tanbro / sqlalchemy-dlock

Distributed lock based on Database and SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
13 stars 0 forks source link

PostgreSQL xact lock in context manager produces warning #4

Open stinovlas opened 1 month ago

stinovlas commented 1 month ago

Hi! When I use following code, it produces warnings:

# `db` is my database connection
with create_sadlock(db, "my_key3", xact=True):
    pass
/my/env/python3.11/site-packages/sqlalchemy_dlock/lock/postgresql.py:182: RuntimeWarning: PostgreSQL transaction level advisory locks are held until the current transaction ends; there is no provision for manual release.

This is caused by __exit__ method of the lock calling close, which calls release. However, there's no release for transaction scoped (xact) PostgreSQL advisory lock. It could be fixed by overriding the close method in PostgresqlSadLock and checking for self._xact value.

tanbro commented 3 weeks ago

It a bug.

For a transaction level PG advisory lock, there is no unlock or release. So, a warning message is shown when invoking release or close. But the warning is shown mistakly in the with statement, because close is called implicitly in __exit__.