|
| 1 | +From c578437c8882fd259a01adee666b2ffbd755cac7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Seppo Yli-Olli < [email protected]> |
| 3 | +Date: Fri, 16 Sep 2022 20:39:05 +0300 |
| 4 | +Subject: [PATCH] Use subprocess umask support |
| 5 | + |
| 6 | +This requires Python 3.9 or newer |
| 7 | + |
| 8 | +diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py |
| 9 | +index 9d058a28e..229230237 100644 |
| 10 | +--- a/src/buildstream/utils.py |
| 11 | ++++ b/src/buildstream/utils.py |
| 12 | +@@ -13,12 +13,6 @@ |
| 13 | + # |
| 14 | + # Authors: |
| 15 | + # Tristan Van Berkom < [email protected]> |
| 16 | +- |
| 17 | +-# Disable this for the file, because pylint is not picking it up |
| 18 | +-# when specifying it on the specific line. |
| 19 | +-# |
| 20 | +-# pylint: disable=subprocess-popen-preexec-fn |
| 21 | +-# |
| 22 | + """ |
| 23 | + Utilities |
| 24 | + ========= |
| 25 | +@@ -1260,14 +1254,7 @@ def _call(*popenargs, terminate=False, **kwargs): |
| 26 | + |
| 27 | + process = None |
| 28 | + |
| 29 | +- old_preexec_fn = kwargs.get("preexec_fn") |
| 30 | +- if "preexec_fn" in kwargs: |
| 31 | +- del kwargs["preexec_fn"] |
| 32 | +- |
| 33 | +- def preexec_fn(): |
| 34 | +- os.umask(stat.S_IWGRP | stat.S_IWOTH) |
| 35 | +- if old_preexec_fn is not None: |
| 36 | +- old_preexec_fn() |
| 37 | ++ kwargs.setdefault("umask", stat.S_IWGRP | stat.S_IWOTH) |
| 38 | + |
| 39 | + # Handle termination, suspend and resume |
| 40 | + def kill_proc(): |
| 41 | +@@ -1313,7 +1300,7 @@ def _call(*popenargs, terminate=False, **kwargs): |
| 42 | + os.killpg(group_id, signal.SIGCONT) |
| 43 | + |
| 44 | + with _signals.suspendable(suspend_proc, resume_proc), _signals.terminator(kill_proc), subprocess.Popen( |
| 45 | +- *popenargs, preexec_fn=preexec_fn, universal_newlines=True, **kwargs |
| 46 | ++ *popenargs, universal_newlines=True, **kwargs |
| 47 | + ) as process: |
| 48 | + # Here, we don't use `process.communicate()` directly without a timeout |
| 49 | + # This is because, if we were to do that, and the process would never |
| 50 | +-- |
| 51 | +2.45.2.windows.1 |
| 52 | + |
| 53 | +From 797c827d455b97710048aad2084fd2957396beef Mon Sep 17 00:00:00 2001 |
| 54 | +From: =?UTF-8?q?J=C3=BCrg=20Billeter?= < [email protected]> |
| 55 | +Date: Fri, 20 Dec 2024 16:49:00 +0100 |
| 56 | +Subject: [PATCH] casdprocessmanager.py: Don't use `preexec_fn` on Python 3.11+ |
| 57 | + |
| 58 | +The use of `preexec_fn` is not generally safe when using multiple |
| 59 | +threads. |
| 60 | + |
| 61 | +diff --git a/src/buildstream/_cas/casdprocessmanager.py b/src/buildstream/_cas/casdprocessmanager.py |
| 62 | +index 5cc64853e..d1157dd93 100644 |
| 63 | +--- a/src/buildstream/_cas/casdprocessmanager.py |
| 64 | ++++ b/src/buildstream/_cas/casdprocessmanager.py |
| 65 | +@@ -20,6 +20,7 @@ import random |
| 66 | + import shutil |
| 67 | + import stat |
| 68 | + import subprocess |
| 69 | ++import sys |
| 70 | + import tempfile |
| 71 | + import time |
| 72 | + from subprocess import CalledProcessError |
| 73 | +@@ -126,16 +127,21 @@ class CASDProcessManager: |
| 74 | + self._start_time = time.time() |
| 75 | + self._logfile = self._rotate_and_get_next_logfile() |
| 76 | + |
| 77 | ++ # Create a new process group for buildbox-casd such that SIGINT won't reach it. |
| 78 | ++ if sys.version_info >= (3, 11): |
| 79 | ++ process_group_kwargs = {"process_group": 0} |
| 80 | ++ else: |
| 81 | ++ process_group_kwargs = {"preexec_fn": os.setpgrp} |
| 82 | ++ |
| 83 | + with open(self._logfile, "w", encoding="utf-8") as logfile_fp: |
| 84 | + # The frontend will take care of terminating buildbox-casd. |
| 85 | +- # Create a new process group for it such that SIGINT won't reach it. |
| 86 | +- self.process = subprocess.Popen( # pylint: disable=consider-using-with, subprocess-popen-preexec-fn |
| 87 | ++ self.process = subprocess.Popen( # pylint: disable=consider-using-with |
| 88 | + casd_args, |
| 89 | + cwd=path, |
| 90 | + stdout=logfile_fp, |
| 91 | + stderr=subprocess.STDOUT, |
| 92 | +- preexec_fn=os.setpgrp, |
| 93 | + env=self.__buildbox_casd_env(), |
| 94 | ++ **process_group_kwargs |
| 95 | + ) |
| 96 | + |
| 97 | + self._casd_channel = None |
| 98 | +-- |
| 99 | +2.45.2.windows.1 |
| 100 | + |
0 commit comments