From 37c8a704796497197e67a881d2e9236c805def4e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 11 Mar 2022 07:13:29 -0500 Subject: [PATCH] fix(build): lock around logging and archiving portions (#2262) * fix: lock around logging and archiving portions Signed-off-by: Henry Schreiner * refactor: fewer locks Signed-off-by: Henry Schreiner --- pyodide-build/pyodide_build/buildall.py | 29 +++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pyodide-build/pyodide_build/buildall.py b/pyodide-build/pyodide_build/buildall.py index 8ed30c3f0..94a275a45 100755 --- a/pyodide-build/pyodide_build/buildall.py +++ b/pyodide-build/pyodide_build/buildall.py @@ -15,7 +15,7 @@ from pathlib import Path from queue import PriorityQueue, Queue from threading import Lock, Thread from time import perf_counter, sleep -from typing import Any, Optional +from typing import Any, ClassVar, Optional from . import common from .buildpkg import needs_rebuild @@ -79,6 +79,9 @@ class StdLibPackage(BasePackage): @total_ordering class Package(BasePackage): + # If the CWD matters, hold this lock + cwd_lock: ClassVar[Lock] = Lock() + def __init__(self, pkgdir: Path): self.pkgdir = pkgdir @@ -120,6 +123,9 @@ class Package(BasePackage): return None def build(self, outputdir: Path, args) -> None: + with self.__class__.cwd_lock: + log_dir = Path(args.log_dir).resolve() if args.log_dir else None + with open(self.pkgdir / "build.log.tmp", "w") as f: p = subprocess.run( [ @@ -162,9 +168,10 @@ class Package(BasePackage): if rebuilt: shutil.move(self.pkgdir / "build.log.tmp", self.pkgdir / "build.log") - if args.log_dir and (self.pkgdir / "build.log").exists(): + if log_dir and (self.pkgdir / "build.log").exists(): shutil.copy( - self.pkgdir / "build.log", Path(args.log_dir) / f"{self.name}.log" + self.pkgdir / "build.log", + log_dir / f"{self.name}.log", ) else: (self.pkgdir / "build.log.tmp").unlink() @@ -181,11 +188,12 @@ class Package(BasePackage): if self.library: return if self.shared_library: - file_path = shutil.make_archive( - f"{self.name}-{self.version}", "zip", self.pkgdir / "dist" - ) - shutil.copy(file_path, outputdir) - Path(file_path).unlink() + with self.__class__.cwd_lock: + file_path = shutil.make_archive( + f"{self.name}-{self.version}", "zip", self.pkgdir / "dist" + ) + shutil.copy(file_path, outputdir) + Path(file_path).unlink() return shutil.copy(self.wheel_path(), outputdir) test_path = self.tests_path() @@ -380,9 +388,8 @@ def build_from_graph(pkg_map: dict[str, BasePackage], outputdir: Path, args) -> built_queue: Queue = Queue() thread_lock = Lock() queue_idx = 1 - package_set: dict[ - str, None - ] = {} # using dict keys for insertion order preservation + # Using dict keys for insertion order preservation + package_set: dict[str, None] = {} def builder(n): nonlocal queue_idx