- Move some of the code into a try..finally block - Shutdown the socket before closing it Signed-off-by: Michael Goldish <mgoldish@xxxxxxxxxx> --- client/tests/kvm/kvm_vm.py | 45 ++++++++++++++++++++++++------------------- 1 files changed, 25 insertions(+), 20 deletions(-) diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py index 82f1eb4..a8d96ca 100755 --- a/client/tests/kvm/kvm_vm.py +++ b/client/tests/kvm/kvm_vm.py @@ -465,7 +465,7 @@ class VM: end_time = time.time() + timeout while time.time() < end_time: try: - o += s.recv(16384) + o += s.recv(1024) if o.splitlines()[-1].split()[-1] == "(qemu)": return (True, o) except: @@ -481,27 +481,32 @@ class VM: except: logging.debug("Could not connect to monitor socket") return (1, "") - status, data = read_up_to_qemu_prompt(s, timeout) - if not status: - s.close() - logging.debug("Could not find (qemu) prompt; output so far:" \ - + kvm_utils.format_str_for_message(data)) - return (1, "") - # Send command - s.sendall(command + "\n") - # Receive command output - data = "" - if block: + + # Send the command and get the resulting output + try: status, data = read_up_to_qemu_prompt(s, timeout) - data = "\n".join(data.splitlines()[1:]) if not status: - s.close() - logging.debug("Could not find (qemu) prompt after command;" - " output so far: %s", - kvm_utils.format_str_for_message(data)) - return (1, data) - s.close() - return (0, data) + logging.debug("Could not find (qemu) prompt; output so far:" + + kvm_utils.format_str_for_message(data)) + return (1, "") + # Send command + s.sendall(command + "\n") + # Receive command output + data = "" + if block: + status, data = read_up_to_qemu_prompt(s, timeout) + data = "\n".join(data.splitlines()[1:]) + if not status: + logging.debug("Could not find (qemu) prompt after command; " + "output so far:" + + kvm_utils.format_str_for_message(data)) + return (1, data) + return (0, data) + + # Clean up before exiting + finally: + s.shutdown(socket.SHUT_RDWR) + s.close() def destroy(self, gracefully=True): -- 1.5.4.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html