From nobody Thu May 2 15:00:40 2024 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; dkim=fail; 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 1528304070024974.8200405992177; Wed, 6 Jun 2018 09:54:30 -0700 (PDT) Received: from localhost ([::1]:53586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQbhZ-0003Hr-Tt for importer@patchew.org; Wed, 06 Jun 2018 12:54:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38493) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQbgi-0002xV-KZ for qemu-devel@nongnu.org; Wed, 06 Jun 2018 12:53:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fQbge-0002jE-Ml for qemu-devel@nongnu.org; Wed, 06 Jun 2018 12:53:32 -0400 Received: from mail-qk0-x242.google.com ([2607:f8b0:400d:c09::242]:37626) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fQbge-0002io-Hl for qemu-devel@nongnu.org; Wed, 06 Jun 2018 12:53:28 -0400 Received: by mail-qk0-x242.google.com with SMTP id j12-v6so4389670qkk.4 for ; Wed, 06 Jun 2018 09:53:28 -0700 (PDT) Received: from x1.local ([138.117.48.222]) by smtp.gmail.com with ESMTPSA id p72-v6sm24644997qkl.32.2018.06.06.09.53.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Jun 2018 09:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XJNXMfArxho3CakW0231vKcsmD1AI1VCa4y1rcWP9RU=; b=hue3n0gvJpJ2LCJ0E09NSHHvZdKbdR4yYldMgOgpyVQ5rY9rIYWBb13WuU/84BKYrH uzMy7MD/dnEBvgWqPYnwTAfZCrFRo864+luRS4axDigryKqQFJREA0leJrwT52UkuXNN 3aMtgz0XosNlSe5Os29opSFlvD6FaRx24GPHawB5GHWg8rsWpkJUIUwOhigs2JN8CEKH 8oT6zDxdeF6vRw4lZyYVUX/o1XkNPskp/+jawzdcBcqmxUmKVxap3E+FchyrEtm38Acu cd1+fjfjq6Zn8fe0I33KZcJ3eAxM8zdpgxxwqbBkZN0oniYEU3Qvc4lM+pWyh+UQrdjB nNuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=XJNXMfArxho3CakW0231vKcsmD1AI1VCa4y1rcWP9RU=; b=J/9JwXdCKSAjqpm/o4g1I/5nb8GIV8ppwry6TDEnKbc+UkkB+03VNbNEdRwIAmhbup cy4HoQB66knjvmmrNW5SKaDkc1l+0iAneo7pyrlopWaluk7gFaz3OsNv6G1/DZtoYrLf a4xeFluHTHP7ymSiCIytAx4AjWyFnKzMbuyph1/coIHfFu4UR2UTZNs+hjdAAGu8PoI1 1W1sT56Hq8A4za0j44mepci0zu5DczapoK2vV8KtykPvtMSLKeCc06zmWCzQbsMdxDpW kmnEmOsyOYLyuQFwJTnyERURosLvLPjLYx1LWBg2I+faBuff81iNwVtUkKaihdvbuZ+0 EGDg== X-Gm-Message-State: APt69E2V87Puv30cF6KpmViMpyIPK20EfsJq7FCOzv3wXKJN3IM0fmZq +I64yZZBOhYXmoLfbcNg2Vk= X-Google-Smtp-Source: ADUXVKLcw64HalzVVIXRfByIAPziq1KkIkHGnfPnPak1fIdq9ypqW8tgQ07zUEZMGWnz2NqHjwxBDA== X-Received: by 2002:a37:d7cf:: with SMTP id t76-v6mr3197621qkt.96.1528304007909; Wed, 06 Jun 2018 09:53:27 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Eduardo Habkost , Cleber Rosa , Markus Armbruster Date: Wed, 6 Jun 2018 13:53:19 -0300 Message-Id: <20180606165319.27056-1-f4bug@amsat.org> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::242 Subject: [Qemu-devel] [RFC PATCH] qmp.py: Fix exception parsing partial JSON 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: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 The readline() call returns partial data. Keep appending until the JSON buffer is complete. This fixes: $ scripts/qmp/qmp-shell -v -p /tmp/qmp.sock Traceback (most recent call last): File "scripts/qmp/qmp-shell", line 456, in main() File "scripts/qmp/qmp-shell", line 441, in main qemu.connect(negotiate) File "scripts/qmp/qmp-shell", line 284, in connect self._greeting =3D super(QMPShell, self).connect(negotiate) File "scripts/qmp/qmp.py", line 143, in connect return self.__negotiate_capabilities() File "scripts/qmp/qmp.py", line 71, in __negotiate_capabilities greeting =3D self.__json_read() File "scripts/qmp/qmp.py", line 85, in __json_read resp =3D json.loads(data) File "/usr/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end =3D self.raw_decode(s, idx=3D_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end =3D self.scan_once(s, idx) ValueError: Expecting object: line 1 column 3 (char 2) Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- Daniel suggested this is due to blocking i/o. I'm sure there is a nicer/more pythonic way to do this, but this works for = me, sorry :) scripts/qmp/qmp.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 5c8cf6a056..e8b55dfcc4 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -78,11 +78,16 @@ class QEMUMonitorProtocol(object): raise QMPCapabilitiesError =20 def __json_read(self, only_event=3DFalse): + data =3D "" while True: - data =3D self.__sockfile.readline() - if not data: + tmp =3D self.__sockfile.readline() + if not tmp: return - resp =3D json.loads(data) + data +=3D tmp + try: + resp =3D json.loads(data) + except ValueError: + continue if 'event' in resp: self.logger.debug("<<< %s", resp) self.__events.append(resp) --=20 2.17.1