From 0fe285ff2592ae35e8238927979d7dfa1fa05ed8 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 15 Mar 2018 01:58:36 +0300 Subject: Acquire a lock to a preference file before working with it Otherwise we can meet a situation, when one process is writing a file, but another one tries to read the same file: prefs.py:147:load:EOFError Traceback (most recent call last): File "/usr/bin/targetcli", line 121, in main() File "/usr/bin/targetcli", line 77, in main shell = TargetCLI('~/.targetcli') File "/usr/lib/python2.7/site-packages/configshell_fb/shell.py", line 176, in __init__ self.prefs.load() File "/usr/lib/python2.7/site-packages/configshell_fb/prefs.py", line 147, in load self._prefs = six.moves.cPickle.load(fsock) EOFError Local variables in innermost frame: self: fsock: filename: '/root/.targetcli/prefs.bin' Signed-off-by: Andrei Vagin --- configshell/prefs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/configshell/prefs.py b/configshell/prefs.py index 248cc1d..7faa1fe 100644 --- a/configshell/prefs.py +++ b/configshell/prefs.py @@ -16,6 +16,7 @@ under the License. ''' import six +import fcntl class Prefs(object): ''' @@ -128,6 +129,7 @@ class Prefs(object): if filename is not None: fsock = open(filename, 'wb') + fcntl.lockf(fsock, fcntl.LOCK_UN) try: six.moves.cPickle.dump(self._prefs, fsock, 2) finally: @@ -143,6 +145,7 @@ class Prefs(object): if filename is not None: fsock = open(filename, 'rb') + fcntl.lockf(fsock, fcntl.LOCK_SH) try: self._prefs = six.moves.cPickle.load(fsock) finally: -- cgit v1.2.1