mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Corbet <corbet@lwn.net>
To: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Linux Doc Mailing List <linux-doc@vger.kernel.org>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Akira Yokosawa <akiyks@gmail.com>,
	Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 01/24] scripts/jobserver-exec: move the code to a class
Date: Thu, 18 Sep 2025 10:58:08 -0600	[thread overview]
Message-ID: <87segj7l5r.fsf@trenco.lwn.net> (raw)
In-Reply-To: <4749921b75d4e0bd85a25d4d94aa2c940fad084e.1758196090.git.mchehab+huawei@kernel.org>

Mauro Carvalho Chehab <mchehab+huawei@kernel.org> writes:

> +class JobserverExec:
> +    """
> +    Claim all slots from make using POSIX Jobserver.
> +
> +    The main methods here are:
> +    - open(): reserves all slots;
> +    - close(): method returns all used slots back to make;
> +    - run(): executes a command setting PARALLELISM=<available slots jobs + 1>
> +    """
> +
> +    def __init__(self):
> +        """Initialize internal vars"""
> +        self.claim = 0
> +        self.jobs = b""
> +        self.reader = None
> +        self.writer = None
> +        self.is_open = False
> +
> +    def open(self):
> +        """Reserve all available slots to be claimed later on"""
> +
> +        if self.is_open:
> +            return
> +
> +        try:
> +            # Fetch the make environment options.
> +            flags = os.environ["MAKEFLAGS"]
> +            # Look for "--jobserver=R,W"
> +            # Note that GNU Make has used --jobserver-fds and --jobserver-auth
> +            # so this handles all of them.
> +            opts = [x for x in flags.split(" ") if x.startswith("--jobserver")]
> +
> +            # Parse out R,W file descriptor numbers and set them nonblocking.
> +            # If the MAKEFLAGS variable contains multiple instances of the
> +            # --jobserver-auth= option, the last one is relevant.
> +            fds = opts[-1].split("=", 1)[1]
> +
> +            # Starting with GNU Make 4.4, named pipes are used for reader
> +            # and writer.
> +            # Example argument: --jobserver-auth=fifo:/tmp/GMfifo8134
> +            _, _, path = fds.partition("fifo:")
> +
> +            if path:
> +                self.reader = os.open(path, os.O_RDONLY | os.O_NONBLOCK)
> +                self.writer = os.open(path, os.O_WRONLY)
> +            else:
> +                self.reader, self.writer = [int(x) for x in fds.split(",", 1)]
> +                # Open a private copy of reader to avoid setting nonblocking
> +                # on an unexpecting process with the same reader fd.
> +                self.reader = os.open("/proc/self/fd/%d" % (self.reader),
> +                                      os.O_RDONLY | os.O_NONBLOCK)
> +
> +            # Read out as many jobserver slots as possible
> +            while True:
> +                try:
> +                    slot = os.read(self.reader, 8)
> +                    self.jobs += slot
> +                except (OSError, IOError) as e:
> +                    if e.errno == errno.EWOULDBLOCK:
> +                        # Stop at the end of the jobserver queue.
> +                        break
> +                    # If something went wrong, give back the jobs.
> +                    if self.jobs:
> +                        os.write(self.writer, self.jobs)
> +                    raise e
> +
> +            # Add a bump for our caller's reserveration, since we're just going
> +            # to sit here blocked on our child.
> +            self.claim = len(self.jobs) + 1
> +
> +        except (KeyError, IndexError, ValueError, OSError, IOError):
> +            # Any missing environment strings or bad fds should result in just
> +            # not being parallel.
> +            self.claim = None

Sigh ... this kind of massive try/except block is one of the best ways
I've found in Python to hide bugs - and Python has a few of those.  I
realize this is existing code, this is not the place to fix it, but I
would like to clean that up at some point.

jon

  reply	other threads:[~2025-09-18 16:58 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-18 11:54 [PATCH v8 00/24] Split sphinx call logic from docs Makefile Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 01/24] scripts/jobserver-exec: move the code to a class Mauro Carvalho Chehab
2025-09-18 16:58   ` Jonathan Corbet [this message]
2025-09-18 17:33     ` Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 02/24] scripts/jobserver-exec: move its class to the lib directory Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 03/24] scripts/jobserver-exec: add a help message Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 04/24] scripts: check-variable-fonts.sh: convert to Python Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 05/24] tools/docs: check-variable-fonts.py: split into a lib and an exec file Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 06/24] check-variable-fonts.py: add a helper to display instructions Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 07/24] scripts: sphinx-pre-install: move it to tools/docs Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 08/24] tools/docs: sphinx-pre-install: drop a debug print Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 09/24] tools/docs: sphinx-pre-install: allow check for alternatives and bail out Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 10/24] tools/docs: python_version: move version check from sphinx-pre-install Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 11/24] tools/docs: sphinx-build-wrapper: add a wrapper for sphinx-build Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 12/24] docs: parallel-wrapper.sh: remove script Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 13/24] docs: Makefile: document latex/PDF PAPER= parameter Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 14/24] docs: Makefile: document FONTS_CONF_DENY_VF= parameter Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 15/24] tools/docs: sphinx-build-wrapper: add an argument for LaTeX interactive mode Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 16/24] tools/docs: sphinx-build-wrapper: allow building PDF files in parallel Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 17/24] tools/docs,scripts: sphinx-*: prevent sphinx-build crashes Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 18/24] tools/docs: sphinx-build-wrapper: Fix output for duplicated names Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 19/24] docs: add support to build manpages from kerneldoc output Mauro Carvalho Chehab
2025-09-27 20:35   ` Nicolas Schier
2025-09-29  0:44     ` Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 20/24] tools: kernel-doc: add a see also section at man pages Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 21/24] scripts: kdoc_parser.py: warn about Python version only once Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 22/24] tools/docs: sphinx-build-wrapper: move rust doc builder to wrapper Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 23/24] tools/docs: sphinx-* break documentation bulds on openSUSE Mauro Carvalho Chehab
2025-09-18 11:54 ` [PATCH v8 24/24] tools/docs: sphinx-build-wrapper: add support to run inside venv Mauro Carvalho Chehab
2025-09-18 17:47 ` [PATCH v8 00/24] Split sphinx call logic from docs Makefile Jonathan Corbet
2025-09-18 19:23   ` Mauro Carvalho Chehab
2025-09-18 19:33     ` Jonathan Corbet
2025-09-18 23:39       ` Mauro Carvalho Chehab
2025-10-17 20:13 ` Jonathan Corbet
2025-10-18 10:41   ` Mauro Carvalho Chehab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87segj7l5r.fsf@trenco.lwn.net \
    --to=corbet@lwn.net \
    --cc=akiyks@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab+huawei@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®