From nobody Sun Sep 28 16:34:02 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1756758536; cv=none; d=zohomail.com; s=zohoarc; b=jZMGGZwgS6LuFi0rimIkS+ic77rvQcXqBZKFvleYYRFduwcw28f2lt11WqBZS19BTBnDG6NWf8JnDiV/DMVCBx7F8XE/k4y4mNvZ1b2MaZz0oluMlu4MHQE07owB5c1zSBopZCp3Q+fae4/np04+8/9CraQIHoEv04EJpr4VWdY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756758536; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Se0q5+uT6KWiejFo7/IaGRao5fvt636my4wPKmetJz0=; b=NLVuCGTQ2mvEwKAA5686fAk64tbaFhpWroZiNUlKQkGC8AqSV9mho/a5MHtEtk5ckVFKgAcNvKahQ4mGEzrY2o0q9BVeKZ9c73IYYS/SeARLRDFKQWUoYjPemcp/aMPiisEd6jN6qPFDKJR0mldBanrrSIniYI/sMGwPz0N5qcE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756758536031338.7713168939813; Mon, 1 Sep 2025 13:28:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utB7v-0004Hb-BL; Mon, 01 Sep 2025 16:27:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1utB7s-0004H8-Ld for qemu-devel@nongnu.org; Mon, 01 Sep 2025 16:27:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1utB7r-00008U-25 for qemu-devel@nongnu.org; Mon, 01 Sep 2025 16:27:40 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-Z9gLCwudODqDvUw21qrhJA-1; Mon, 01 Sep 2025 16:27:37 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2ABF0180035F; Mon, 1 Sep 2025 20:27:36 +0000 (UTC) Received: from jsnow-thinkpadp16vgen1.westford.csb (unknown [10.22.80.85]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C76319560B4; Mon, 1 Sep 2025 20:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756758458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Se0q5+uT6KWiejFo7/IaGRao5fvt636my4wPKmetJz0=; b=KvGxIfCeVcEN1dVbSnAWOpYVXNh5Wx4o6Pcmj0PDe85ZM4HmpSSJo+jfjc4qsDLZaO+73F gqsQlxjxjUSkR3L7Q5O3zFAhqWZkmKI93JRCs6jeiZf3zMhkpozy+Oy7yGHwv6S5TNTJ5J ew/KHf1ku+obBP6awQsaf1zm11Weoj0= X-MC-Unique: Z9gLCwudODqDvUw21qrhJA-1 X-Mimecast-MFC-AGG-ID: Z9gLCwudODqDvUw21qrhJA_1756758456 From: John Snow To: qemu-devel@nongnu.org Cc: Hanna Reitz , Cleber Rosa , John Snow , qemu-block@nongnu.org, =?UTF-8?q?Daniel=20Berrang=C3=A9?= , Kevin Wolf , "Richard W.M. Jones" Subject: [PATCH 12/19] python: backport '*really* remove get_event_loop' Date: Mon, 1 Sep 2025 16:26:54 -0400 Message-ID: <20250901202702.2971212-13-jsnow@redhat.com> In-Reply-To: <20250901202702.2971212-1-jsnow@redhat.com> References: <20250901202702.2971212-1-jsnow@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1756758539423124100 A prior commit, aa1ff990, switched away from using get_event_loop *by default*, but this is not good enough to avoid deprecation warnings as `asyncio.get_event_loop_policy().get_event_loop()` is *also* deprecated. Replace this mechanism with explicit calls to asyncio.get_new_loop() and revise the cleanup mechanisms in __del__ to match. Reported-by: Richard W.M. Jones Reported-by: Daniel P. Berrang=C3=A9 Signed-off-by: John Snow cherry picked from commit 21ce2ee4f2df87efe84a27b9c5112487f4670622 Signed-off-by: John Snow --- python/qemu/qmp/legacy.py | 47 +++++++++++++++++++++++++++----------- python/qemu/qmp/qmp_tui.py | 10 ++++++-- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/python/qemu/qmp/legacy.py b/python/qemu/qmp/legacy.py index 735d42971e9..775b1fdd3b3 100644 --- a/python/qemu/qmp/legacy.py +++ b/python/qemu/qmp/legacy.py @@ -86,13 +86,15 @@ def __init__(self, "server argument should be False when passing a socket") =20 self._qmp =3D QMPClient(nickname) + self._created_loop =3D False =20 try: self._aloop =3D asyncio.get_running_loop() except RuntimeError: - # No running loop; since this is a sync shim likely to be - # used in fully sync programs, create one if neccessary. - self._aloop =3D asyncio.get_event_loop_policy().get_event_loop= () + # No running loop; since this is a sync shim likely to be used + # in sync programs without any event loop at all, create one. + self._aloop =3D asyncio.new_event_loop() + self._created_loop =3D True =20 self._address =3D address self._timeout: Optional[float] =3D None @@ -313,17 +315,36 @@ def send_fd_scm(self, fd: int) -> None: self._qmp.send_fd_scm(fd) =20 def __del__(self) -> None: - if self._qmp.runstate =3D=3D Runstate.IDLE: - return + if self._qmp.runstate !=3D Runstate.IDLE: + self._qmp.logger.warning( + "QEMUMonitorProtocol object garbage collected without a pr= ior " + "call to close()" + ) =20 if not self._aloop.is_running(): - self.close() - else: - # Garbage collection ran while the event loop was running. - # Nothing we can do about it now, but if we don't raise our - # own error, the user will be treated to a lot of traceback - # they might not understand. + if self._qmp.runstate !=3D Runstate.IDLE: + # If the user neglected to close the QMP session and we + # are not currently running in an asyncio context, we + # have the opportunity to close the QMP session. If we + # do not do this, the error messages presented over + # dangling async resources may not make any sense to the + # user. + self.close() + + # If we created our own loop (and we are not running inside + # of it), we must close it to avoid warnings and error + # messages upon program exit. + if self._created_loop: + self._aloop.close() + + if self._qmp.runstate !=3D Runstate.IDLE: + # If QMP is still not quiesced, it means that the garbage + # collector ran from a context within the event loop and we + # are simply too late to take any corrective action. Raise + # our own error to give meaningful feedback to the user in + # order to prevent pages of asyncio stacktrace jargon. raise QMPError( - "QEMUMonitorProtocol.close()" - " was not called before object was garbage collected" + "QEMUMonitorProtocol.close() was not called before object = was " + "garbage collected, and could not be closed due to GC runn= ing " + "in the event loop" ) diff --git a/python/qemu/qmp/qmp_tui.py b/python/qemu/qmp/qmp_tui.py index 12bdc17c99e..d5526338f22 100644 --- a/python/qemu/qmp/qmp_tui.py +++ b/python/qemu/qmp/qmp_tui.py @@ -161,6 +161,7 @@ def __init__(self, address: Union[str, Tuple[str, int]]= , num_retries: int, self.retry_delay =3D retry_delay if retry_delay else 2 self.retry: bool =3D False self.exiting: bool =3D False + self._created_loop =3D False super().__init__() =20 def add_to_history(self, msg: str, level: Optional[str] =3D None) -> N= one: @@ -391,8 +392,9 @@ def run(self, debug: bool =3D False) -> None: try: self.aloop =3D asyncio.get_running_loop() except RuntimeError: - # No running asyncio event loop. Create one if necessary. - self.aloop =3D asyncio.get_event_loop_policy().get_event_loop() + # No running asyncio event loop. Create one. + self.aloop =3D asyncio.new_event_loop() + self._created_loop =3D True =20 self.aloop.set_debug(debug) =20 @@ -416,6 +418,10 @@ def run(self, debug: bool =3D False) -> None: logging.error('%s\n%s\n', str(err), pretty_traceback()) raise err =20 + def __del__(self) -> None: + if self._created_loop and self.aloop: + self.aloop.close() + =20 class StatusBar(urwid.Text): """ --=20 2.50.1