From patchwork Wed Sep 9 18:12:09 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 46436 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n89IFr30019208 for ; Wed, 9 Sep 2009 18:15:53 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753987AbZIISPp (ORCPT ); Wed, 9 Sep 2009 14:15:45 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753986AbZIISPp (ORCPT ); Wed, 9 Sep 2009 14:15:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3486 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753965AbZIISPo (ORCPT ); Wed, 9 Sep 2009 14:15:44 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFk7k019066; Wed, 9 Sep 2009 14:15:46 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFkV2004987; Wed, 9 Sep 2009 14:15:46 -0400 Received: from localhost.localdomain (dhcp-1-188.tlv.redhat.com [10.35.1.188]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFNFb012008; Wed, 9 Sep 2009 14:15:44 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [PATCH 16/19] KVM test: kvm_subprocess: robustify the test for child process termination Date: Wed, 9 Sep 2009 21:12:09 +0300 Message-Id: <1252519932-30733-16-git-send-email-mgoldish@redhat.com> In-Reply-To: <1252519932-30733-15-git-send-email-mgoldish@redhat.com> References: <1252519932-30733-1-git-send-email-mgoldish@redhat.com> <1252519932-30733-2-git-send-email-mgoldish@redhat.com> <1252519932-30733-3-git-send-email-mgoldish@redhat.com> <1252519932-30733-4-git-send-email-mgoldish@redhat.com> <1252519932-30733-5-git-send-email-mgoldish@redhat.com> <1252519932-30733-6-git-send-email-mgoldish@redhat.com> <1252519932-30733-7-git-send-email-mgoldish@redhat.com> <1252519932-30733-8-git-send-email-mgoldish@redhat.com> <1252519932-30733-9-git-send-email-mgoldish@redhat.com> <1252519932-30733-10-git-send-email-mgoldish@redhat.com> <1252519932-30733-11-git-send-email-mgoldish@redhat.com> <1252519932-30733-12-git-send-email-mgoldish@redhat.com> <1252519932-30733-13-git-send-email-mgoldish@redhat.com> <1252519932-30733-14-git-send-email-mgoldish@redhat.com> <1252519932-30733-15-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Don't rely on os.read() raising an exception; use waitpid() to check for child termination: - every 0.5 seconds, or - when os.read() raises an exception, or - when os.read() returns a zero-length string. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_subprocess.py | 17 ++++++++++++----- 1 files changed, 12 insertions(+), 5 deletions(-) diff --git a/client/tests/kvm/kvm_subprocess.py b/client/tests/kvm/kvm_subprocess.py index d142ed9..c3c48cd 100755 --- a/client/tests/kvm/kvm_subprocess.py +++ b/client/tests/kvm/kvm_subprocess.py @@ -1102,10 +1102,11 @@ def _server_main(): # Read from child and write to files/pipes while True: + check_termination = False # Make a list of reader pipes whose buffers are not empty fds = [fd for (i, fd) in enumerate(reader_fds) if buffers[i]] # Wait until there's something to do - r, w, x = select.select([shell_fd, inpipe_fd], fds, []) + r, w, x = select.select([shell_fd, inpipe_fd], fds, [], 0.5) # If a reader pipe is ready for writing -- for (i, fd) in enumerate(reader_fds): if fd in w: @@ -1116,7 +1117,9 @@ def _server_main(): try: data = os.read(shell_fd, 16384) except OSError: - break + data = "" + if not data: + check_termination = True # Remove carriage returns from the data -- they often cause # trouble and are normally not needed data = data.replace("\r", "") @@ -1124,14 +1127,18 @@ def _server_main(): output_file.flush() for i in range(len(readers)): buffers[i] += data + # If os.read() raised an exception or there was nothing to read -- + if check_termination or shell_fd not in r: + pid, status = os.waitpid(shell_pid, os.WNOHANG) + if pid: + status = os.WEXITSTATUS(status) + break # If there's data to read from the client -- if inpipe_fd in r: data = os.read(inpipe_fd, 1024) os.write(shell_fd, data) - # Wait for the shell process to exit and get its exit status - status = os.waitpid(shell_pid, 0)[1] - status = os.WEXITSTATUS(status) + # Write the exit status to a file file = open(status_filename, "w") file.write(str(status)) file.close()