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.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id E06CE15815E for ; Wed, 7 Feb 2024 02:35:22 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 1A8BBE2A0F; Wed, 7 Feb 2024 02:35:22 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id F2E08E2A0F for ; Wed, 7 Feb 2024 02:35:21 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 122313430EA for ; Wed, 7 Feb 2024 02:35:21 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 712DC14D0 for ; Wed, 7 Feb 2024 02:35:18 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1707267346.3e10368e52ecead86b75478ca448ef0f59333e3e.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/process.py X-VCS-Directories: lib/portage/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 3e10368e52ecead86b75478ca448ef0f59333e3e X-VCS-Branch: master Date: Wed, 7 Feb 2024 02:35:18 +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: e0fa1c05-3f84-4830-814c-c798918b1335 X-Archives-Hash: 1dc5614a560fe50943331900a9cf7553 commit: 3e10368e52ecead86b75478ca448ef0f59333e3e Author: Zac Medico gentoo org> AuthorDate: Sun Feb 4 04:34:45 2024 +0000 Commit: Zac Medico gentoo org> CommitDate: Wed Feb 7 00:55:46 2024 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=3e10368e process.spawn: Use _start_proc for returnpid=False This essentially completes the implementation of bug 916566, eliminating os.fork() usage when "spawn" becomes the default multiprocessing start method. Bug: https://bugs.gentoo.org/916566 Signed-off-by: Zac Medico gentoo.org> lib/portage/process.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/portage/process.py b/lib/portage/process.py index 1ca59ee594..a33e7b4747 100644 --- a/lib/portage/process.py +++ b/lib/portage/process.py @@ -586,10 +586,10 @@ def spawn( # Create a tee process, giving it our stdout and stderr # as well as the read end of the pipe. - mypids.extend( + mypids.append( spawn( ("tee", "-i", "-a", logfile), - returnpid=True, + returnproc=True, fd_pipes={0: pr, 1: fd_pipes[1], 2: fd_pipes[2]}, ) ) @@ -634,7 +634,7 @@ def spawn( # fork, so that the result is cached in the main process. bool(groups) - start_func = _start_proc if returnproc else _start_fork + start_func = _start_proc if returnproc or not returnpid else _start_fork pid = start_func( _exec_wrapper, @@ -666,7 +666,7 @@ def spawn( # _start_proc returns a MultiprocessingProcess instance. return pid - if not isinstance(pid, int): + if returnpid and not isinstance(pid, int): raise AssertionError(f"fork returned non-integer: {repr(pid)}") # Add the pid to our local and the global pid lists. @@ -687,6 +687,8 @@ def spawn( ) return mypids + loop = global_event_loop() + # Otherwise we clean them up. while mypids: # Pull the last reader in the pipe chain. If all processes @@ -695,25 +697,22 @@ def spawn( pid = mypids.pop(0) # and wait for it. - retval = os.waitpid(pid, 0)[1] + retval = loop.run_until_complete(pid.wait()) if retval: # If it failed, kill off anything else that # isn't dead yet. for pid in mypids: - # With waitpid and WNOHANG, only check the - # first element of the tuple since the second - # element may vary (bug #337465). - if os.waitpid(pid, os.WNOHANG)[0] == 0: - os.kill(pid, signal.SIGTERM) - os.waitpid(pid, 0) - - # If it got a signal, return the signal that was sent. - if retval & 0xFF: - return (retval & 0xFF) << 8 - - # Otherwise, return its exit code. - return retval >> 8 + waiter = asyncio.ensure_future(pid.wait(), loop) + try: + loop.run_until_complete( + asyncio.wait_for(asyncio.shield(waiter), 0.001) + ) + except (TimeoutError, asyncio.TimeoutError): + pid.terminate() + loop.run_until_complete(waiter) + + return retval # Everything succeeded return 0