Skip to content

Commit e80b5ee

Browse files
Merge pull request #111 from ARMmbed/devel_lockfile_timeout
Replace lockfile context mgr usage with lock.aquire() and lock.release()
2 parents d38fd72 + b772236 commit e80b5ee

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

mbed_lstools/lstools_base.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import lockfile
2323
from os import listdir
2424
from os.path import isfile, join
25-
from lockfile import LockFailed
25+
from lockfile import LockFailed, LockTimeout
2626

2727
class MbedLsToolsBase:
2828
""" 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):
252252
return {}
253253

254254
try:
255-
with self.mbedls_get_global_lock():
255+
lock = self.mbedls_get_global_lock()
256+
if lock.acquire(timeout=0.5):
256257
# This read is for backward compatibility
257258
# When user already have on its system local mock-up it will work
258259
# overwriting global one
259260
if isfile(self.MOCK_FILE_NAME):
260-
return read_mock_file(self.MOCK_FILE_NAME)
261+
ret = read_mock_file(self.MOCK_FILE_NAME)
262+
lock.release()
263+
return ret
261264

262265
if isfile(self.MOCK_HOME_FILE_NAME):
263-
return read_mock_file(self.MOCK_HOME_FILE_NAME)
264-
except LockFailed as e:
266+
ret = read_mock_file(self.MOCK_HOME_FILE_NAME)
267+
lock.release()
268+
return ret
269+
except (LockFailed, LockTimeout) as e:
265270
self.err(str(e))
266271
return {}
267272

@@ -284,9 +289,12 @@ def write_mock_file(filename, mock_ids):
284289
return False
285290

286291
try:
287-
with self.mbedls_get_global_lock():
288-
return write_mock_file(self.MOCK_HOME_FILE_NAME, mock_ids)
289-
except LockFailed as e:
292+
lock = self.mbedls_get_global_lock()
293+
if lock.acquire(timeout=0.5):
294+
ret = write_mock_file(self.MOCK_HOME_FILE_NAME, mock_ids)
295+
lock.release()
296+
return ret
297+
except (LockFailed, LockTimeout) as e:
290298
self.err(str(e))
291299
return False
292300

0 commit comments

Comments
 (0)