From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41974) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQe5v-0003d8-K5 for qemu-devel@nongnu.org; Wed, 06 Jun 2018 15:27:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fQe5q-0002nQ-M2 for qemu-devel@nongnu.org; Wed, 06 Jun 2018 15:27:43 -0400 Received: from mail-qk0-x22e.google.com ([2607:f8b0:400d:c09::22e]:39698) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fQe5q-0002nB-Gq for qemu-devel@nongnu.org; Wed, 06 Jun 2018 15:27:38 -0400 Received: by mail-qk0-x22e.google.com with SMTP id g14-v6so4731796qkm.6 for ; Wed, 06 Jun 2018 12:27:38 -0700 (PDT) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Date: Wed, 6 Jun 2018 16:27:31 -0300 Message-Id: <20180606192731.6910-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [RFC PATCH v2] qmp.py: Fix exception parsing partial JSON List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eduardo Habkost , Cleber Rosa , Markus Armbruster Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org, =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= 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 = 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 = self.__json_read() File "scripts/qmp/qmp.py", line 85, in __json_read resp = 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 = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting object: line 1 column 3 (char 2) Signed-off-by: Philippe Mathieu-Daudé --- Since v1: - addressed Daniel review: clean data after json.loads() succeeds - add a XXX comment 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 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 5c8cf6a056..14f0b48936 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -78,11 +78,18 @@ class QEMUMonitorProtocol(object): raise QMPCapabilitiesError def __json_read(self, only_event=False): + data = "" while True: - data = self.__sockfile.readline() - if not data: + tmp = self.__sockfile.readline() + if not tmp: return - resp = json.loads(data) + data += tmp + try: + resp = json.loads(data) + except ValueError: + # XXX: blindly loop, even if QEMU ever sends malformed data + continue + data = "" if 'event' in resp: self.logger.debug("<<< %s", resp) self.__events.append(resp) -- 2.17.1