def acquire(self): """Acquire the lock""" open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC fd = None try: # Under Windows, filesystem.open will raise directly an EACCES error # if the lock file is already locked. fd = filesystem.open(self._path, open_mode, 0o600) # The need for this "type: ignore" was fixed in # https://github.com/python/typeshed/pull/3607 and included in # newer versions of mypy so it can be removed when mypy is # upgraded. msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) # type: ignore except (IOError, OSError) as err: if fd: os.close(fd) # Anything except EACCES is unexpected. Raise directly the error in that case. if err.errno != errno.EACCES: raise logger.debug('A lock on %s is held by another process.', self._path) raise errors.LockError( 'Another instance of Certbot is already running.') self._fd = fd
def acquire(self): """Acquire the lock""" open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC fd = None try: # Under Windows, filesystem.open will raise directly an EACCES error # if the lock file is already locked. fd = filesystem.open(self._path, open_mode, 0o600) # This "type: ignore" is currently needed because msvcrt methods # are only defined on Windows. See # https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stdlib/msvcrt.pyi. msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) # type: ignore except (IOError, OSError) as err: if fd: os.close(fd) # Anything except EACCES is unexpected. Raise directly the error in that case. if err.errno != errno.EACCES: raise logger.debug('A lock on %s is held by another process.', self._path) raise errors.LockError( 'Another instance of Certbot is already running.') self._fd = fd
def _try_lock(self, fd): # type: (int) -> None """ Try to acquire the lock file without blocking. :param int fd: file descriptor of the opened file to lock """ try: fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as err: if err.errno in (errno.EACCES, errno.EAGAIN): logger.debug('A lock on %s is held by another process.', self._path) raise errors.LockError('Another instance of Certbot is already running.') raise
def _try_lock(self, fd): """Try to acquire the lock file without blocking. :param int fd: file descriptor of the opened file to lock """ try: compat.lock_file(fd) except IOError as err: if err.errno in (errno.EACCES, errno.EAGAIN): logger.debug("A lock on %s is held by another process.", self._path) raise errors.LockError( "Another instance of Certbot is already running.") raise
def acquire(self): """Acquire the lock""" open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC fd = os.open(self._path, open_mode, 0o600) try: msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) except (IOError, OSError) as err: os.close(fd) # Anything except EACCES is unexpected. Raise directly the error in that case. if err.errno != errno.EACCES: raise logger.debug('A lock on %s is held by another process.', self._path) raise errors.LockError( 'Another instance of Certbot is already running.') self._fd = fd