def acquire(self, hostId, lease): if lease != self._lease: raise MultipleLeasesNotSupported("acquire", lease) with self._globalLockMapSync: self.log.info("Acquiring local lock for domain %s (id: %s)", self._sdUUID, hostId) hostId, lockFile = self._getLease() if lockFile: try: osutils.uninterruptible(fcntl.fcntl, lockFile, fcntl.F_GETFD) except IOError as e: # We found a stale file descriptor, removing. del self._globalLockMap[self._sdUUID] # Raise any other unkown error. if e.errno != errno.EBADF: raise else: self.log.debug("Local lock already acquired for domain " "%s (id: %s)", self._sdUUID, hostId) return # success, the lock was already acquired lockFile = osutils.uninterruptible(os.open, self._idsPath, os.O_RDONLY) try: osutils.uninterruptible(fcntl.flock, lockFile, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as e: osutils.close_fd(lockFile) if e.errno in (errno.EACCES, errno.EAGAIN): raise se.AcquireLockFailure(self._sdUUID, e.errno, "Cannot acquire local lock", str(e)) raise else: self._globalLockMap[self._sdUUID] = (hostId, lockFile) self.log.debug("Local lock for domain %s successfully acquired " "(id: %s)", self._sdUUID, hostId)
def test_close(self): fds = os.pipe() for fd in fds: osutils.close_fd(fd) time.sleep(0.1) for fd in fds: path = "/proc/self/fd/%d" % fd self.assertFalse(os.path.exists(path))
def release(self, lease): if lease != self._lease: raise MultipleLeasesNotSupported("release", lease) with self._globalLockMapSync: self.log.info("Releasing local lock for domain %s", self._sdUUID) hostId, lockFile = self._getLease() if not lockFile: self.log.debug("Local lock already released for domain %s", self._sdUUID) return osutils.close_fd(lockFile) self._globalLockMap[self._sdUUID] = (hostId, None) self.log.debug("Local lock for domain %s successfully released", self._sdUUID)
def acquire(self, hostId, lease): if lease != self._lease: raise MultipleLeasesNotSupported("acquire", lease) with self._globalLockMapSync: self.log.info("Acquiring local lock for domain %s (id: %s)", self._sdUUID, hostId) hostId, lockFile = self._getLease() if lockFile: try: osutils.uninterruptible(fcntl.fcntl, lockFile, fcntl.F_GETFD) except IOError as e: # We found a stale file descriptor, removing. del self._globalLockMap[self._sdUUID] # Raise any other unkown error. if e.errno != errno.EBADF: raise else: self.log.debug( "Local lock already acquired for domain " "%s (id: %s)", self._sdUUID, hostId) return # success, the lock was already acquired lockFile = osutils.uninterruptible(os.open, self._idsPath, os.O_RDONLY) try: osutils.uninterruptible(fcntl.flock, lockFile, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as e: osutils.close_fd(lockFile) if e.errno in (errno.EACCES, errno.EAGAIN): raise se.AcquireLockFailure(self._sdUUID, e.errno, "Cannot acquire local lock", str(e)) raise else: self._globalLockMap[self._sdUUID] = (hostId, lockFile) self.log.debug( "Local lock for domain %s successfully acquired " "(id: %s)", self._sdUUID, hostId)
def wrapper(*a, **kw): r, w = os.pipe() try: pid = os.fork() if pid == 0: try: f(*a, **kw) os._exit(0) except Exception as e: os.write(w, pickle.dumps(e)) os._exit(1) else: _, status = os.waitpid(pid, 0) if status != 0: e = pickle.loads(os.read(r, 4006)) raise e finally: osutils.close_fd(r) osutils.close_fd(w)
def wrapper(*a, **kw): r, w = os.pipe() try: pid = os.fork() if pid == 0: try: f(*a, **kw) os._exit(0) except Exception as e: os.write(w, pickle.dumps(e)) os._exit(1) else: _, status = osutils.uninterruptible(os.waitpid, pid, 0) if status != 0: e = pickle.loads(os.read(r, 4006)) raise e finally: osutils.close_fd(r) osutils.close_fd(w)
def test_propagate_other_errors(self): with self.assertRaises(OSError) as e: osutils.close_fd(-1) self.assertEqual(e.exception.errno, errno.EBADF)