Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Locking edge case when lock_path does not exist
Browse files Browse the repository at this point in the history
Fixes bug 1158179

In the case that the lock_path does not exist and there is a contested
resource then the process that was waiting on the lock will not
be locking due to the fact that the directory was deleted.

Change-Id: I75d720d4df499e85386d3e2cc86b927b017e12ac
  • Loading branch information
Gary Kotton committed Mar 24, 2013
1 parent 1c0f660 commit 3cb71ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
1 change: 0 additions & 1 deletion openstack/common/lockutils.py
Expand Up @@ -207,7 +207,6 @@ def inner(*args, **kwargs):
local_lock_path = tempfile.mkdtemp()

if not os.path.exists(local_lock_path):
cleanup_dir = True
fileutils.ensure_tree(local_lock_path)

# NOTE(mikal): the lock name cannot contain directory
Expand Down
30 changes: 26 additions & 4 deletions tests/unit/test_lockutils.py
Expand Up @@ -128,7 +128,7 @@ def inner_lock():
if os.path.exists(tempdir):
shutil.rmtree(tempdir)

def test_synchronized_externally(self):
def _do_test_synchronized_externally(self):
"""We can lock across multiple processes"""
tempdir = tempfile.mkdtemp()
self.config(lock_path=tempdir)
Expand Down Expand Up @@ -162,20 +162,42 @@ def lock_files(tempdir):
# Check if we were able to open all files
self.assertEqual(50, count)

handles_dir = tempfile.mkdtemp()
try:
children = []
for n in range(50):
pid = os.fork()
if pid:
children.append(pid)
else:
lock_files(tempdir)
lock_files(handles_dir)
os._exit(0)

for i, child in enumerate(children):
(pid, status) = os.waitpid(child, 0)
if pid:
self.assertEqual(0, status)
finally:
if os.path.exists(tempdir):
shutil.rmtree(tempdir, ignore_errors=True)
if os.path.exists(handles_dir):
shutil.rmtree(handles_dir, ignore_errors=True)

def test_synchronized_externally(self):
lock_dir = tempfile.mkdtemp()
self.config(lock_path=lock_dir)

try:
self._do_test_synchronized_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)

def test_synchronized_externally_lock_dir_not_exist(self):
lock_dir = tempfile.mkdtemp()
os.rmdir(lock_dir)
self.config(lock_path=lock_dir)

try:
self._do_test_synchronized_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)

0 comments on commit 3cb71ff

Please sign in to comment.