Skip to content

Commit

Permalink
fix EOFError when execute targetcli commands concurrently
Browse files Browse the repository at this point in the history
1.fcntl.LOCK_UN means unlock, but save file must own the exclusive lock.
  So, we must use fcntl.LOCK_EX(acquire an exclusive lock) flag in here.
2.Then, fcntl.lockf is different from fcntl.flock:
  a)fcntl.flock always lock for the whole file
  b)fcntl.lockf can only lock part of the file
  c)see https://docs.python.org/2.7/library/fcntl.html for details
  I think we need to call fcntl.flock in here to avoid EOFError.

Signed-off-by: Zou Mingzhe <[email protected]>
  • Loading branch information
zoumingzhe committed Oct 20, 2021
1 parent d74a33b commit 9f31078
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions configshell/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ def save(self, filename=None):

if filename is not None:
fsock = open(filename, 'wb')
fcntl.lockf(fsock, fcntl.LOCK_UN)
try:
fcntl.flock(fsock, fcntl.LOCK_EX)
six.moves.cPickle.dump(self._prefs, fsock, 2)
fsock.flush()
finally:
fcntl.flock(fsock, fcntl.LOCK_UN)
fsock.close()

def load(self, filename=None):
Expand All @@ -146,8 +148,9 @@ def load(self, filename=None):

if filename is not None and os.path.exists(filename):
fsock = open(filename, 'rb')
fcntl.lockf(fsock, fcntl.LOCK_SH)
try:
fcntl.flock(fsock, fcntl.LOCK_SH)
self._prefs = six.moves.cPickle.load(fsock)
finally:
fcntl.flock(fsock, fcntl.LOCK_UN)
fsock.close()

0 comments on commit 9f31078

Please sign in to comment.