To avoid waiting for the full 15-second timeout for a connection, check if QEMU exited once in a while. Start with a very short timeout (15ms), and double it on each try.
This will be helpful for scripts/device-crash-test, on cases where QEMU crashes or exits before connecting to the QMP socket. Signed-off-by: Eduardo Habkost <ehabk...@redhat.com> --- scripts/qemu.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/qemu.py b/scripts/qemu.py index 880e3e8219..5416bcfef9 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -17,6 +17,7 @@ import string import os import sys import subprocess +import socket import qmp.qmp @@ -118,7 +119,18 @@ class QEMUMachine(object): debug=self._debug) def _post_launch(self): - self._qmp.accept() + # Wait for a connection while checking if QEMU is still running: + timeout = 0.015 + tries = 10 # 0.015*2**10 = ~15 seconds + for i in range(tries): + try: + self._qmp.accept(timeout=timeout) + break + except socket.timeout: + if self._popen.poll(): + raise Exception("QEMU terminated before connecting to QMP") + # wait a little longer on the next try: + timeout *= 2 def _post_shutdown(self): if not isinstance(self._monitor_address, tuple): -- 2.11.0.259.g40922b1