Merge "Fix named locks implementation"

This commit is contained in:
Zuul 2017-10-25 13:20:55 +00:00 committed by Gerrit Code Review
commit a741d03fad
1 changed files with 10 additions and 6 deletions

View File

@ -1623,10 +1623,14 @@ def delete_named_lock(lock_id, session=None):
@contextlib.contextmanager
def named_lock(name):
lock_id = None
# NOTE(rakhmerov): We can't use the well-known try-finally pattern here
# because if lock creation failed then it means that the SQLAlchemy
# session is no longer valid and we can't use to try to delete the lock.
# All we can do here is to let the exception bubble up so that the
# transaction management code could rollback the transaction.
try:
lock_id = create_named_lock(name)
yield
finally:
delete_named_lock(lock_id)
lock_id = create_named_lock(name)
yield
delete_named_lock(lock_id)