Make singleinstance() more robust on OS X

It now handles EINTR and also does not hide all errors in opening the
file or calling lockf()

An error is now reported to the user in a nice error dialog before
aborting startup.
This commit is contained in:
Kovid Goyal 2014-12-19 13:28:59 +05:30
parent e486e67d57
commit f8aea31f40
2 changed files with 15 additions and 6 deletions

View File

@ -459,8 +459,14 @@ def main(args=sys.argv):
app, opts, args = init_qt(args)
except AbortInit:
return 1
try:
from calibre.utils.lock import singleinstance
si = singleinstance(singleinstance_name)
except Exception:
error_dialog(None, _('Cannot start calibre'), _(
'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'),
det_msg=traceback.format_exc(), show=True)
return 1
if si and opts.shutdown_running_calibre:
return 0
if si:

View File

@ -233,12 +233,15 @@ else:
@param name: The name to lock.
@type name: string
'''
from calibre.utils.ipc import eintr_retry_call
path = singleinstance_path(name)
try:
f = open(path, 'w')
fcntl.lockf(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
try:
eintr_retry_call(fcntl.lockf, f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
atexit.register(_clean_lock_file, f)
return True
except EnvironmentError:
pass
except IOError as err:
if err.errno == errno.EAGAIN:
return False
raise
return False