From patchwork Mon Nov 7 20:38:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9416019 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8DC3A6022E for ; Mon, 7 Nov 2016 20:38:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EBF128D24 for ; Mon, 7 Nov 2016 20:38:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7125B28E1D; Mon, 7 Nov 2016 20:38:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EBD1028D24 for ; Mon, 7 Nov 2016 20:38:49 +0000 (UTC) Received: from localhost ([::1]:56470 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c3qgr-0005Gx-7p for patchwork-qemu-devel@patchwork.kernel.org; Mon, 07 Nov 2016 15:38:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44503) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c3qgT-0005EX-0G for qemu-devel@nongnu.org; Mon, 07 Nov 2016 15:38:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c3qgS-0004MF-4k for qemu-devel@nongnu.org; Mon, 07 Nov 2016 15:38:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41980) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c3qgK-0004By-LY; Mon, 07 Nov 2016 15:38:16 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BD8254E047; Mon, 7 Nov 2016 20:38:15 +0000 (UTC) Received: from red.redhat.com (ovpn-116-189.phx2.redhat.com [10.3.116.189]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uA7KcEoW012675; Mon, 7 Nov 2016 15:38:15 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 7 Nov 2016 14:38:13 -0600 Message-Id: <1478551093-32757-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 07 Nov 2016 20:38:15 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] nbd: Don't inf-loop on early EOF X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop. Rework the loop to accomodate reads cut short by EOF. Reported-by: Max Reitz Signed-off-by: Eric Blake Reviewed-by: Max Reitz --- nbd/client.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 5d94e34..29b6edf 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -79,20 +79,21 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); * the amount of bytes consumed. */ static ssize_t drop_sync(QIOChannel *ioc, size_t size) { - ssize_t ret, dropped = size; + ssize_t ret = 0; char small[1024]; char *buffer; buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size)); while (size > 0) { - ret = read_sync(ioc, buffer, MIN(65536, size)); - if (ret < 0) { + ssize_t count = read_sync(ioc, buffer, MIN(65536, size)); + + if (count <= 0) { goto cleanup; } - assert(ret <= size); - size -= ret; + assert(count <= size); + size -= count; + ret += count; } - ret = dropped; cleanup: if (buffer != small) {