From 441ce6ba3cfe729df2daa0f417c53bf350a1ef42 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 19 Jul 2016 15:02:11 +0100 Subject: [PATCH 1/2] Replace lockfile context mgr usage with lock.aquire() and lock.release() To catch LockTImeout exception we can't use context mgr. We need to use explicit calls to acquire(timeout=1) and release() to catch timeout. TImeout may occur on some Resource Managers instance. LockFile objects support the context manager protocol used by the statement:with statement. The timeout option is not supported when used in this fashion. While support for timeouts could be implemented, there is no support for handling the eventual Timeout exceptions raised by the __enter__() method, so you would have to protect the with statement with a try statement. The resulting construct would not be any simpler than just using a try statement in the first place. --- mbed_lstools/lstools_base.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/mbed_lstools/lstools_base.py b/mbed_lstools/lstools_base.py index 62ae3f4..8021755 100644 --- a/mbed_lstools/lstools_base.py +++ b/mbed_lstools/lstools_base.py @@ -22,7 +22,7 @@ import lockfile from os import listdir from os.path import isfile, join -from lockfile import LockFailed +from lockfile import LockFailed, LockTimeout class MbedLsToolsBase: """ Base class for mbed-lstools, defines mbed-ls tools interface for mbed-enabled devices detection for various hosts @@ -252,16 +252,21 @@ def read_mock_file(filename): return {} try: - with self.mbedls_get_global_lock(): + lock = self.mbedls_get_global_lock() + if lock.acquire(timeout=1): # This read is for backward compatibility # When user already have on its system local mock-up it will work # overwriting global one if isfile(self.MOCK_FILE_NAME): - return read_mock_file(self.MOCK_FILE_NAME) + ret = read_mock_file(self.MOCK_FILE_NAME) + lock.release() + return ret if isfile(self.MOCK_HOME_FILE_NAME): - return read_mock_file(self.MOCK_HOME_FILE_NAME) - except LockFailed as e: + ret = read_mock_file(self.MOCK_HOME_FILE_NAME) + lock.release() + return ret + except (LockFailed, LockTimeout) as e: self.err(str(e)) return {} @@ -284,9 +289,12 @@ def write_mock_file(filename, mock_ids): return False try: - with self.mbedls_get_global_lock(): - return write_mock_file(self.MOCK_HOME_FILE_NAME, mock_ids) - except LockFailed as e: + lock = self.mbedls_get_global_lock() + if lock.acquire(timeout=1): + ret = write_mock_file(self.MOCK_HOME_FILE_NAME, mock_ids) + lock.release() + return ret + except (LockFailed, LockTimeout) as e: self.err(str(e)) return False From b772236fa7b4643c900c7625b1f54bc3f288dc80 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 19 Jul 2016 15:08:30 +0100 Subject: [PATCH 2/2] Reduced timeout for mock file Locking to 0.5 seconds --- mbed_lstools/lstools_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mbed_lstools/lstools_base.py b/mbed_lstools/lstools_base.py index 8021755..e218e10 100644 --- a/mbed_lstools/lstools_base.py +++ b/mbed_lstools/lstools_base.py @@ -253,7 +253,7 @@ def read_mock_file(filename): try: lock = self.mbedls_get_global_lock() - if lock.acquire(timeout=1): + if lock.acquire(timeout=0.5): # This read is for backward compatibility # When user already have on its system local mock-up it will work # overwriting global one @@ -290,7 +290,7 @@ def write_mock_file(filename, mock_ids): try: lock = self.mbedls_get_global_lock() - if lock.acquire(timeout=1): + if lock.acquire(timeout=0.5): ret = write_mock_file(self.MOCK_HOME_FILE_NAME, mock_ids) lock.release() return ret