From cf0cc8bb874530531eef518d8510eee60eab0f7a Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 18 May 2019 19:31:41 +0700 Subject: [PATCH] Ensure Qemu monitor commands are executed. Ref #1582. --- gns3server/compute/qemu/qemu_vm.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/gns3server/compute/qemu/qemu_vm.py b/gns3server/compute/qemu/qemu_vm.py index ef4e56eb..9c0433d8 100644 --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -1081,7 +1081,15 @@ class QemuVM(BaseNode): return result try: - writer.write(command.encode('ascii') + b"\n") + cmd_byte = command.encode('ascii') + writer.write(cmd_byte + b"\n") + if not expected: + while True: + line = await asyncio.wait_for(reader.readline(), timeout=3) # echo of the command + if not line or cmd_byte in line: + break + except asyncio.TimeoutError: + log.warning("Missing echo of command '{}'".format(command)) except OSError as e: log.warning("Could not write to QEMU monitor: {}".format(e)) writer.close() @@ -1089,13 +1097,15 @@ class QemuVM(BaseNode): if expected: try: while result is None: - line = await reader.readline() + line = await asyncio.wait_for(reader.readline(), timeout=3) if not line: break for expect in expected: if expect in line: result = line.decode("utf-8").strip() break + except asyncio.TimeoutError: + log.warning("Timeout while waiting for result of command '{}'".format(command)) except (ConnectionError, EOFError) as e: log.warning("Could not read from QEMU monitor: {}".format(e)) writer.close() @@ -1118,6 +1128,9 @@ class QemuVM(BaseNode): log.info("Execute QEMU monitor command: {}".format(command)) try: writer.write(command.encode('ascii') + b"\n") + await asyncio.wait_for(reader.readline(), timeout=3) # echo of the command + except asyncio.TimeoutError: + log.warning("Missing echo of command '{}'".format(command)) except OSError as e: log.warning("Could not write to QEMU monitor: {}".format(e)) writer.close()