Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix EOFError when execute targetcli commands concurrently #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions configshell/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Prefs(object):
autosave = False
__borg_state = {}

def __init__(self, filename=None):
def __init__(self, filename=None, lockfile=None):
'''
Instanciates the ConfigShell preferences object.
@param filename: File to store the preferencces to.
Expand All @@ -42,6 +42,11 @@ def __init__(self, filename=None):
self.__dict__ = self.__borg_state
if filename is not None:
self.filename = filename
if lockfile is None:
prefs_dir = os.getenv("TARGETCLI_HOME", '~/.targetcli')
prefs_dir = os.path.expanduser(prefs_dir)
lockfile = os.path.join(prefs_dir, 'prefs.lock')
self.lockfile = lockfile

def __getitem__(self, key):
'''
Expand Down Expand Up @@ -129,12 +134,20 @@ def save(self, filename=None):
filename = self.filename

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

def load(self, filename=None):
'''
Expand All @@ -144,10 +157,17 @@ def load(self, filename=None):
if filename is None:
filename = self.filename

if filename is not None and os.path.exists(filename):
fsock = open(filename, 'rb')
fcntl.lockf(fsock, fcntl.LOCK_SH)
if filename is not None and os.path.isfile(filename):
flk = open(self.lockfile, 'wb')
fcntl.flock(flk, fcntl.LOCK_SH)
try:
self._prefs = six.moves.cPickle.load(fsock)
fpref = open(filename, 'rb')
try:
fcntl.flock(fpref, fcntl.LOCK_SH)
self._prefs = six.moves.cPickle.load(fpref)
finally:
fcntl.flock(fpref, fcntl.LOCK_UN)
fpref.close()
finally:
fsock.close()
fcntl.flock(flk, fcntl.LOCK_UN)
flk.close()
3 changes: 2 additions & 1 deletion configshell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def __init__(self, preferences_dir=None):
if not os.path.exists(preferences_dir):
os.makedirs(preferences_dir)
self._prefs_file = preferences_dir + '/prefs.bin'
self.prefs = prefs.Prefs(self._prefs_file)
self._prefs_lock = preferences_dir + '/prefs.lock'
self.prefs = prefs.Prefs(self._prefs_file, self._prefs_lock)
self._cmd_history = preferences_dir + '/history.txt'
self._save_history = True
if not os.path.isfile(self._cmd_history):
Expand Down