From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id C2ECE1382C5 for ; Thu, 10 Jun 2021 01:35:45 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0AE9AE0932; Thu, 10 Jun 2021 01:35:45 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id C55FCE0932 for ; Thu, 10 Jun 2021 01:35:44 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 1AF14335DB0 for ; Thu, 10 Jun 2021 01:35:43 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 0F35979A for ; Thu, 10 Jun 2021 01:35:41 +0000 (UTC) From: "Matt Turner" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Matt Turner" Message-ID: <1623288932.8a9780b8bef53d427fa7d68b7df85e2d34f4dd57.mattst88@gentoo> Subject: [gentoo-commits] proj/catalyst:wip/mattst88 commit in: catalyst/base/, catalyst/, catalyst/targets/ X-VCS-Repository: proj/catalyst X-VCS-Files: catalyst/base/clearbase.py catalyst/base/stagebase.py catalyst/lock.py catalyst/targets/snapshot.py X-VCS-Directories: catalyst/base/ catalyst/ catalyst/targets/ X-VCS-Committer: mattst88 X-VCS-Committer-Name: Matt Turner X-VCS-Revision: 8a9780b8bef53d427fa7d68b7df85e2d34f4dd57 X-VCS-Branch: wip/mattst88 Date: Thu, 10 Jun 2021 01:35:41 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: da9c8663-32ca-46f3-b57e-28410672dec7 X-Archives-Hash: 4e5b4abeae08a71afadc37591a2665c7 commit: 8a9780b8bef53d427fa7d68b7df85e2d34f4dd57 Author: Matt Turner gentoo org> AuthorDate: Wed Jun 9 06:17:31 2021 +0000 Commit: Matt Turner gentoo org> CommitDate: Thu Jun 10 01:35:32 2021 +0000 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=8a9780b8 catalyst: Replace snakeoil's locks with fasteners To no great surprise, the existing locking was broken. For example, clear_chroot() releases the lock. It is called by unpack(), which is part of prepare_sequence. The result is that the whole build could be done without holding the lock. Just lock around run(). It's not apparent that finer-grained locking does anything for us. Signed-off-by: Matt Turner gentoo.org> catalyst/base/clearbase.py | 2 -- catalyst/base/stagebase.py | 10 +++----- catalyst/lock.py | 58 -------------------------------------------- catalyst/targets/snapshot.py | 6 ++--- 4 files changed, 7 insertions(+), 69 deletions(-) diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py index dcf6c523..6218330e 100644 --- a/catalyst/base/clearbase.py +++ b/catalyst/base/clearbase.py @@ -27,12 +27,10 @@ class ClearBase(): self.resume.clear_all(remove=True) def clear_chroot(self): - self.chroot_lock.unlock() log.notice('Clearing the chroot path ...') clear_dir(self.settings["chroot_path"], mode=0o755) def remove_chroot(self): - self.chroot_lock.unlock() log.notice('Removing the chroot path ...') clear_dir(self.settings["chroot_path"], mode=0o755, remove=True) diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py index 448d6265..34d10389 100644 --- a/catalyst/base/stagebase.py +++ b/catalyst/base/stagebase.py @@ -8,6 +8,7 @@ import sys from pathlib import Path +import fasteners import libmount import toml @@ -25,7 +26,6 @@ from catalyst.support import (CatalystError, file_locate, normpath, from catalyst.base.targetbase import TargetBase from catalyst.base.clearbase import ClearBase from catalyst.base.genbase import GenBase -from catalyst.lock import LockDir, LockInUse from catalyst.fileops import ensure_dirs, clear_dir, clear_path from catalyst.base.resume import AutoResume @@ -36,9 +36,6 @@ def run_sequence(sequence): sys.stdout.flush() try: func() - except LockInUse: - log.error('Unable to aquire the lock...') - return False except Exception: log.error('Exception running action sequence %s', func.__name__, exc_info=True) @@ -478,7 +475,6 @@ class StageBase(TargetBase, ClearBase, GenBase): """ self.settings["chroot_path"] = normpath(self.settings["storedir"] + "/tmp/" + self.settings["target_subpath"].rstrip('/')) - self.chroot_lock = LockDir(self.settings["chroot_path"]) def set_autoresume_path(self): self.settings["autoresume_path"] = normpath(pjoin( @@ -1366,8 +1362,10 @@ class StageBase(TargetBase, ClearBase, GenBase): pass def run(self): - self.chroot_lock.write_lock() + with fasteners.InterProcessLock(self.chroot_path + '.lock'): + return self._run() + def _run(self): if "clear-autoresume" in self.settings["options"]: self.clear_autoresume() diff --git a/catalyst/lock.py b/catalyst/lock.py deleted file mode 100644 index e31745b2..00000000 --- a/catalyst/lock.py +++ /dev/null @@ -1,58 +0,0 @@ - -import os - -from contextlib import contextmanager - -from snakeoil import fileutils -from snakeoil import osutils -from catalyst.fileops import ensure_dirs - - -LockInUse = osutils.LockException - -class Lock: - """ - A fnctl-based filesystem lock - """ - def __init__(self, lockfile): - fileutils.touch(lockfile, mode=0o664) - os.chown(lockfile, uid=-1, gid=250) - self.lock = osutils.FsLock(lockfile) - - def read_lock(self): - self.lock.acquire_read_lock() - - def write_lock(self): - self.lock.acquire_write_lock() - - def unlock(self): - # Releasing a write lock is the same as a read lock. - self.lock.release_write_lock() - -class LockDir(Lock): - """ - A fnctl-based filesystem lock in a directory - """ - def __init__(self, lockdir): - ensure_dirs(lockdir) - lockfile = os.path.join(lockdir, '.catalyst_lock') - - Lock.__init__(self, lockfile) - -@contextmanager -def read_lock(filename): - lock = Lock(filename) - lock.read_lock() - try: - yield - finally: - lock.unlock() - -@contextmanager -def write_lock(filename): - lock = Lock(filename) - lock.write_lock() - try: - yield - finally: - lock.unlock() diff --git a/catalyst/targets/snapshot.py b/catalyst/targets/snapshot.py index b494575a..ef68765d 100644 --- a/catalyst/targets/snapshot.py +++ b/catalyst/targets/snapshot.py @@ -5,11 +5,12 @@ Snapshot target import subprocess import sys +import fasteners + from pathlib import Path from catalyst import log from catalyst.base.targetbase import TargetBase -from catalyst.lock import write_lock from catalyst.support import CatalystError, command class snapshot(TargetBase): @@ -93,8 +94,7 @@ class snapshot(TargetBase): log.notice('>>> ' + ' '.join([*git_cmd, '|'])) log.notice(' ' + ' '.join(tar2sqfs_cmd)) - lockfile = self.snapshot.with_suffix('.lock') - with write_lock(lockfile): + with fasteners.InterProcessLock(self.snapshot.with_suffix('.lock')): git = subprocess.Popen(git_cmd, stdout=subprocess.PIPE, stderr=sys.stderr,