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 560FC158041 for ; Sun, 25 Feb 2024 08:25:15 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8030AE29CD; Sun, 25 Feb 2024 08:25:14 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 685D8E29CD for ; Sun, 25 Feb 2024 08:25:14 +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 8A8B133BF60 for ; Sun, 25 Feb 2024 08:25:13 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 2567414C8 for ; Sun, 25 Feb 2024 08:25:12 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1708849499.92615cd37d7a1efce923c474e455f59fe61a589c.sam@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: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 92615cd37d7a1efce923c474e455f59fe61a589c X-VCS-Branch: master Date: Sun, 25 Feb 2024 08:25:12 +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: c74a83ab-daca-4335-9d77-35187f74787e X-Archives-Hash: ead5cd2c55e904e61e483f7027f55c4e commit: 92615cd37d7a1efce923c474e455f59fe61a589c Author: Zac Medico gentoo org> AuthorDate: Sun Feb 25 05:09:48 2024 +0000 Commit: Sam James gentoo org> CommitDate: Sun Feb 25 08:24:59 2024 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=92615cd3 _start_proc: Prevent premature ForkProcess garbage collection In order to prevent the constructed ForkProcess instance from being prematurely garbage collected, return a reference to the caller inside of a _GCProtector object, preventing messages reported in bug 925456 like this: [ERROR] Task was destroyed but it is pending! Fixes: a69c1b853a47 ("process.spawn: Use multiprocessing.Process for returnproc") Bug: https://bugs.gentoo.org/925456 Signed-off-by: Zac Medico gentoo.org> Closes: https://github.com/gentoo/portage/pull/1284 Signed-off-by: Sam James gentoo.org> lib/portage/process.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/portage/process.py b/lib/portage/process.py index d16262e75a..cc9ed7bf78 100644 --- a/lib/portage/process.py +++ b/lib/portage/process.py @@ -36,6 +36,7 @@ portage.proxy.lazyimport.lazyimport( from portage.const import BASH_BINARY, SANDBOX_BINARY, FAKEROOT_BINARY from portage.exception import CommandNotFound +from portage.proxy.objectproxy import ObjectProxy from portage.util._ctypes import find_library, LoadLibrary, ctypes try: @@ -1493,8 +1494,44 @@ def _start_proc( proc.start() # ForkProcess conveniently holds a MultiprocessingProcess - # instance that is suitable to return here. - return proc._proc + # instance that is suitable to return here, but use _GCProtector + # to protect the ForkProcess instance from being garbage collected + # and triggering messages like this (bug 925456): + # [ERROR] Task was destroyed but it is pending! + return _GCProtector(proc._proc, proc.async_wait) + + +class _GCProtector(ObjectProxy): + """ + Proxy a target object, and also hold a reference to something + extra in order to protect it from garbage collection. Override + the wait method to first call target's wait method and then + wait for extra (a coroutine function) before returning the result. + """ + + __slots__ = ("_extra", "_target") + + def __init__(self, target, extra): + super().__init__() + object.__setattr__(self, "_target", target) + object.__setattr__(self, "_extra", extra) + + def _get_target(self): + return object.__getattribute__(self, "_target") + + def __getattribute__(self, attr): + if attr == "wait": + return object.__getattribute__(self, attr) + return getattr(object.__getattribute__(self, "_target"), attr) + + async def wait(self): + """ + Wrap the target's wait method to also wait for an extra + coroutine function. + """ + result = await object.__getattribute__(self, "_target").wait() + await object.__getattribute__(self, "_extra")() + return result def find_binary(binary):