qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Eduardo Habkost <ehabkost@redhat.com>,
	qemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Cleber Rosa <crosa@redhat.com>, John Snow <jsnow@redhat.com>
Subject: [PATCH 10/10] iotests: ensure that QemuIoInteractive definitely closes
Date: Wed, 12 May 2021 17:46:42 -0400	[thread overview]
Message-ID: <20210512214642.2803189-11-jsnow@redhat.com> (raw)
In-Reply-To: <20210512214642.2803189-1-jsnow@redhat.com>

More on the lines of quieting pylint 2.8.x, though to make it obvious
that we definitely handle the cleanup here, I elected to bolster the
close() method here.

1. Check for the process having terminated early more rigorously by
checking poll() directly.

2. Change the prompt read into an assertion.

3. Ensure that the final communicate() call *definitely* closes the
socket, adding a timeout and a final kill just in case.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 tests/qemu-iotests/iotests.py | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index e09c991b84e..12e876fa67d 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -238,20 +238,27 @@ def qemu_io_silent_check(*args):
 class QemuIoInteractive:
     def __init__(self, *args):
         self.args = qemu_io_args_no_fmt + list(args)
+
+        # Resource cleaned up via close()
+        # pylint: disable=consider-using-with
         self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT,
                                    universal_newlines=True)
+        if self._p.poll():
+            # Failed to start.
+            out = self._p.stdout.read()
+            raise subprocess.SubprocessError(out, self._p.poll())
+
+        # Eat the first prompt
         out = self._p.stdout.read(9)
-        if out != 'qemu-io> ':
-            # Most probably qemu-io just failed to start.
-            # Let's collect the whole output and exit.
-            out += self._p.stdout.read()
-            self._p.wait(timeout=1)
-            raise ValueError(out)
+        assert out == 'qemu-io> ', "Did not understand qemu-io prompt"
 
     def close(self):
-        self._p.communicate('q\n')
+        try:
+            self._p.communicate('q\n', timeout=5)
+        except subprocess.TimeoutExpired:
+            self._p.kill()
 
     def _read_output(self):
         pattern = 'qemu-io> '
-- 
2.30.2



  parent reply	other threads:[~2021-05-12 21:56 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 21:46 [PATCH 00/10] Python: delint iotests, machine.py and console_socket.py John Snow
2021-05-12 21:46 ` [PATCH 01/10] python/console_socket: avoid one-letter variable John Snow
2021-05-13  9:39   ` Philippe Mathieu-Daudé
2021-05-12 21:46 ` [PATCH 02/10] python/machine: use subprocess.DEVNULL instead of open(os.path.devnull) John Snow
2021-05-13  9:39   ` Philippe Mathieu-Daudé
2021-05-12 21:46 ` [PATCH 03/10] python/machine: use subprocess.run instead of subprocess.Popen John Snow
2021-05-14 14:08   ` Wainer dos Santos Moschetta
2021-05-14 19:00     ` John Snow
2021-05-14 19:13     ` John Snow
2021-05-12 21:46 ` [PATCH 04/10] python/console_socket: Add a pylint ignore John Snow
2021-05-12 21:46 ` [PATCH 05/10] python/machine: Disable pylint warning for open() in _pre_launch John Snow
2021-05-14 14:42   ` Wainer dos Santos Moschetta
2021-05-14 19:03     ` John Snow
2021-05-12 21:46 ` [PATCH 06/10] python/machine: disable warning for Popen in _launch() John Snow
2021-05-12 21:46 ` [PATCH 07/10] iotests: use subprocess.run where possible John Snow
2021-05-14 19:22   ` John Snow
2021-05-12 21:46 ` [PATCH 08/10] iotests: use 'with open()' where applicable John Snow
2021-05-13  9:40   ` Philippe Mathieu-Daudé
2021-05-12 21:46 ` [PATCH 09/10] iotests: silence spurious consider-using-with warnings John Snow
2021-05-12 21:46 ` John Snow [this message]
2021-05-17 16:11 ` [PATCH 00/10] Python: delint iotests, machine.py and console_socket.py John Snow
2021-05-17 17:02   ` Emanuele Giuseppe Esposito

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210512214642.2803189-11-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).