From a48283c163f30cdf11573b3c002a8bb3243a6cc9 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Fri, 17 Mar 2017 10:48:03 -0700 Subject: [PATCH] move locking code to separate function --- certbot/main.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/certbot/main.py b/certbot/main.py index 3c911353c..c9553e6ec 100644 --- a/certbot/main.py +++ b/certbot/main.py @@ -867,6 +867,37 @@ def _post_logging_setup(config, plugins, cli_args): logger.debug("Discovered plugins: %r", plugins) +def acquire_file_lock(lock_path): + """Obtain a lock on the file at the specified path. + + :param str lock_path: path to the file to be locked + + :returns: lock file object representing the acquired lock + :rtype: fasteners.InterProcessLock + + :raises .Error: if the lock is held by another process + + """ + lock = fasteners.InterProcessLock(lock_path) + logger.debug("Attempting to acquire lock file %s", lock_path) + + try: + lock.acquire(blocking=False) + except IOError as err: + logger.debug(err) + logger.warning( + "Unable to access lock file %s. You should set --lock-file " + "to a writeable path to ensure multiple instances of " + "Certbot don't attempt modify your configuration " + "simultaneously.", lock_path) + else: + if not lock.acquired: + raise errors.Error( + "Another instance of Certbot is already running.") + + return lock + + def _run_subcommand(config, plugins): """Executes the Certbot subcommand specified in the configuration. @@ -877,15 +908,13 @@ def _run_subcommand(config, plugins): :rtype: str or int """ - lock = fasteners.InterProcessLock(constants.LOCK_FILE) - logger.debug("Attempting to acquire lock file %s", constants.LOCK_FILE) - if not lock.acquire(blocking=False): - raise errors.Error("Another instance of Certbot is already running.") + lock = acquire_file_lock(config.lock_path) try: return config.func(config, plugins) finally: - lock.release() + if lock.acquired: + lock.release() def main(cli_args=sys.argv[1:]):