From f48713f4ae23cc470be2a2d8ce1f9bad35e54bfe Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Mon, 20 May 2019 16:56:16 -0400 Subject: [PATCH] Use lockf, not flock. This performs more reliably on certain filesystems in Linux. --- awx/main/tasks.py | 8 ++++---- awx/main/tests/unit/test_tasks.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index e1f0f77964..e1720cde0c 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -1834,9 +1834,9 @@ class RunProjectUpdate(BaseTask): def release_lock(self, instance): try: - fcntl.flock(self.lock_fd, fcntl.LOCK_UN) + fcntl.lockf(self.lock_fd, fcntl.LOCK_UN) except IOError as e: - logger.error("I/O error({0}) while trying to open lock file [{1}]: {2}".format(e.errno, instance.get_lock_file(), e.strerror)) + logger.error("I/O error({0}) while trying to release lock file [{1}]: {2}".format(e.errno, instance.get_lock_file(), e.strerror)) os.close(self.lock_fd) raise @@ -1852,7 +1852,7 @@ class RunProjectUpdate(BaseTask): raise RuntimeError(u'Invalid lock file path') try: - self.lock_fd = os.open(lock_path, os.O_RDONLY | os.O_CREAT) + self.lock_fd = os.open(lock_path, os.O_RDWR | os.O_CREAT) except OSError as e: logger.error("I/O error({0}) while trying to open lock file [{1}]: {2}".format(e.errno, lock_path, e.strerror)) raise @@ -1864,7 +1864,7 @@ class RunProjectUpdate(BaseTask): if instance.cancel_flag: logger.debug("ProjectUpdate({0}) was cancelled".format(instance.pk)) return - fcntl.flock(self.lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + fcntl.lockf(self.lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) break except IOError as e: if e.errno not in (errno.EAGAIN, errno.EACCES): diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 08d8cbc3d1..143e461daf 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -2248,7 +2248,7 @@ def test_os_open_oserror(): def test_fcntl_ioerror(): with pytest.raises(OSError): - fcntl.flock(99999, fcntl.LOCK_EX) + fcntl.lockf(99999, fcntl.LOCK_EX) @mock.patch('os.open') @@ -2276,8 +2276,8 @@ def test_aquire_lock_open_fail_logged(logging_getLogger, os_open): @mock.patch('os.open') @mock.patch('os.close') @mock.patch('logging.getLogger') -@mock.patch('fcntl.flock') -def test_aquire_lock_acquisition_fail_logged(fcntl_flock, logging_getLogger, os_close, os_open): +@mock.patch('fcntl.lockf') +def test_aquire_lock_acquisition_fail_logged(fcntl_lockf, logging_getLogger, os_close, os_open): err = IOError() err.errno = 3 err.strerror = 'dummy message' @@ -2291,7 +2291,7 @@ def test_aquire_lock_acquisition_fail_logged(fcntl_flock, logging_getLogger, os_ logger = mock.Mock() logging_getLogger.return_value = logger - fcntl_flock.side_effect = err + fcntl_lockf.side_effect = err ProjectUpdate = tasks.RunProjectUpdate() with pytest.raises(IOError, message='dummy message'):