From nobody Tue Feb 10 21:19:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1501003424836870.0212036883518; Tue, 25 Jul 2017 10:23:44 -0700 (PDT) Received: from localhost ([::1]:33778 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1da3Yd-0006T1-GQ for importer@patchew.org; Tue, 25 Jul 2017 13:23:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1da3XC-0005aj-Bz for qemu-devel@nongnu.org; Tue, 25 Jul 2017 13:22:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1da3X8-0002Ox-Ak for qemu-devel@nongnu.org; Tue, 25 Jul 2017 13:22:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36950) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1da3X8-0002Of-1f for qemu-devel@nongnu.org; Tue, 25 Jul 2017 13:22:10 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4358F8E3D5; Tue, 25 Jul 2017 17:12:15 +0000 (UTC) Received: from t460p.pahim.org.com (ovpn-204-234.brq.redhat.com [10.40.204.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DB6B95A67; Tue, 25 Jul 2017 17:12:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4358F8E3D5 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=apahim@redhat.com From: Amador Pahim To: qemu-devel@nongnu.org Date: Tue, 25 Jul 2017 19:10:12 +0200 Message-Id: <20170725171014.25193-5-apahim@redhat.com> In-Reply-To: <20170725171014.25193-1-apahim@redhat.com> References: <20170725171014.25193-1-apahim@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 25 Jul 2017 17:12:15 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v5 4/6] qemu.py: cleanup launch() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, ldoktor@redhat.com, famz@redhat.com, ehabkost@redhat.com, stefanha@gmail.com, Amador Pahim , armbru@redhat.com, mreitz@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" launch() is currently taking care of a number of flows, each one if its own exception treatment, depending on the VM state and the files creation state. This patch makes launch() more resilient, off-loading the core calls to the new _launch() and calling shutdown() if any exception is raised by _launch(), making sure VM will be terminated and cleaned up. Signed-off-by: Amador Pahim --- scripts/qemu.py | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/scripts/qemu.py b/scripts/qemu.py index 56142ed59b..45a63e8e9d 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -99,8 +99,11 @@ class QEMUMachine(object): return self._popen.pid =20 def _load_io_log(self): - with open(self._qemu_log_path, "r") as fh: - self._iolog =3D fh.read() + try: + with open(self._qemu_log_path, "r") as fh: + self._iolog =3D fh.read() + except IOError: + pass =20 def _base_args(self): if isinstance(self._monitor_address, tuple): @@ -126,23 +129,28 @@ class QEMUMachine(object): self._remove_if_exists(self._qemu_log_path) =20 def launch(self): - '''Launch the VM and establish a QMP connection''' - devnull =3D open('/dev/null', 'rb') - qemulog =3D open(self._qemu_log_path, 'wb') + ''' + Try to launch the VM and make sure we cleanup on exception. + ''' + if self.is_running(): + return + try: - self._pre_launch() - args =3D self._wrapper + [self._binary] + self._base_args() + = self.args - self._popen =3D subprocess.Popen(args, stdin=3Ddevnull, stdout= =3Dqemulog, - stderr=3Dsubprocess.STDOUT, she= ll=3DFalse) - self._post_launch() + self._launch() except: - if self.is_running(): - self._popen.kill() - self._popen.wait() - self._load_io_log() - self._post_shutdown() + self.shutdown() raise =20 + def _launch(self): + '''Launch the VM and establish a QMP connection.''' + devnull =3D open('/dev/null', 'rb') + qemulog =3D open(self._qemu_log_path, 'wb') + self._pre_launch() + args =3D self._wrapper + [self._binary] + self._base_args() + self= ._args + self._popen =3D subprocess.Popen(args, stdin=3Ddevnull, stdout=3Dq= emulog, + stderr=3Dsubprocess.STDOUT, shell= =3DFalse) + self._post_launch() + def shutdown(self): '''Terminate the VM and clean up''' if self.is_running(): @@ -156,8 +164,8 @@ class QEMUMachine(object): if exitcode < 0: sys.stderr.write('qemu received signal %i\n' % -exitcode) =20 - self._load_io_log() - self._post_shutdown() + self._load_io_log() + self._post_shutdown() =20 underscore_to_dash =3D string.maketrans('_', '-') def qmp(self, cmd, conv_keys=3DTrue, **args): --=20 2.13.3