From libssh2-devel-bounces@cool.haxx.se  Fri Dec  6 20:14:33 2019
Return-Path: <libssh2-devel-bounces@cool.haxx.se>
Received: from www.haxx.se (mail [127.0.0.1])
	by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTP id xB6JDsqp002103;
	Fri, 6 Dec 2019 20:14:20 +0100
Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com
 [IPv6:2607:f8b0:4864:20:0:0:0:22b])
 by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTPS id xB6JDqoU001942
 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT)
 for <libssh2-devel@cool.haxx.se>; Fri, 6 Dec 2019 20:13:53 +0100
Received: by mail-oi1-x22b.google.com with SMTP id c16so775905oic.3
 for <libssh2-devel@cool.haxx.se>; Fri, 06 Dec 2019 11:13:54 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=mime-version:from:date:message-id:subject:to;
 bh=AtdZzLiyFRpTtTPiNQnGxpwALMt3uaGLEdwj8Nk4YwQ=;
 b=VL5oLXkV15FefjSoD6Wco/N0S2lj0BuvO7lV/VSda3XPsgu93CO/yG+VyTyGNUVHD6
 vWbLjBQO2Dvhgn0O68nvDz9s46vRXZ2lzzao6RDGwHw+FgbnvER5zoUIj67p4UxPHeOj
 5qLEpweqG2kAdgv2xnJmVaexzw3sfEhYyE6xEMBTJL+I6rrLYK8+ZgAc+B7kY/Lh8gZO
 2tw58aAbcTqYXk8/GO4CbzGjEFkZiGoIZEXrxB9SwCCRE4qjhEoceNzxobP1vZ9u59CM
 WVJmLDa3O7Bjv7qe7I9FWUirKFlrZIZH350ph0tJRd+KUjU75/9NPVE8fc+lJtcIiWOy
 3iQw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
 bh=AtdZzLiyFRpTtTPiNQnGxpwALMt3uaGLEdwj8Nk4YwQ=;
 b=nNP1V4t0OCzmwtpAteHkb/ekfCj7eubqD4uQ+rIqo4unhiLiWeZV9ajuoCtM+lC1u3
 XrOWuI07DQJ1k9pxxj1daoGycxvYGPj59Rhqn6SyKaxhxpKxn2UYuKGAxHjk2J+iiTuY
 Tn9QzdMhCMcB5ypkAvxHjpIIdoNznk1KiJVnIIQo530J87rmxOkcT9DgAlRLCl1cjyw/
 ppbXkEtecsRX3UzxhznjiTymCHNrVgjkVhHjjrRVm1PHOCPWkxUskGR/TvzqFFM/C/Hn
 js5YciWbH05SLjPE0XhiwNcm6O5hv4Cr55rxkW1YLq+ctT8/xo7ECZK69ui1HD7lutu3
 rvBg==
X-Gm-Message-State: APjAAAWvcPXnLeALYK947T33UUu9dcbwye+CiFjn+EAqSj1sAN0ScQ/D
 GykSRC4H7KeCaVJrhNcg640Ep10qZ+Bi7fYhqpJwsMHe
X-Google-Smtp-Source: APXvYqy7fm8s0C2oSPiW7hDjSradSIlGNi59UGZVzwUDpZxReHzCzSECgjfcWrCp1mp/2QPhhidptsd5y6mrplg453c=
X-Received: by 2002:aca:411:: with SMTP id 17mr8216245oie.5.1575659628069;
 Fri, 06 Dec 2019 11:13:48 -0800 (PST)
MIME-Version: 1.0
From: Radu Rendec <radu.rendec@gmail.com>
Date: Fri, 6 Dec 2019 14:13:37 -0500
Message-ID: <CAD5jUk9eGwuhjV-9UXkReP+Ejs6Z3_Ph3WXn-JkU_Njh_oCGew@mail.gmail.com>
Subject: Busy loop in libssh2_sftp_write
To: libssh2-devel@cool.haxx.se
X-BeenThere: libssh2-devel@cool.haxx.se
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: libssh2 development <libssh2-devel.cool.haxx.se>
List-Unsubscribe: <https://cool.haxx.se/cgi-bin/mailman/options/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=unsubscribe>
List-Archive: <http://cool.haxx.se/pipermail/libssh2-devel/>
List-Post: <mailto:libssh2-devel@cool.haxx.se>
List-Help: <mailto:libssh2-devel-request@cool.haxx.se?subject=help>
List-Subscribe: <https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=subscribe>
Reply-To: libssh2 development <libssh2-devel@cool.haxx.se>
Content-Type: text/plain; charset="utf-8"
Errors-To: libssh2-devel-bounces@cool.haxx.se
Sender: "libssh2-devel" <libssh2-devel-bounces@cool.haxx.se>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id xB6JDsqp002103

Hi Everyone,

I'm observing some strange behavior in libssh2_sftp_write and I think it
may be going into a busy loop. I'm using an older library version (on an
embedded system), but that part of the code looks identical to the
latest master branch on github.

The busy loop seems to be the loop that the BLOCK_ADJUST macro inserts.
What I see is that sftp_write returns immediately with an error (the
underlying send() call returns with an error as well). Then the call to
_libssh2_wait_socket returns 0, immediately. At a closer look, the
poll() inside _libssh2_wait_socket has both POLLIN and POLLOUT set in
the events bitmask, but only POLLIN set in the returned events bitmask.
That explains why poll() returns immediately and returns 1 and yet the
subsequent call to send() fails.

I get that _libssh2_wait_socket is a generic function, but I believe in
this particular case it should only poll for writing (i.e. POLLOUT),
since the other function call in the outer loop only tries to write.

I assume the socket has some data in the RX queue and the TX queue is
full. How the socket ends up in that state is a different story (and I
have a long and clear explanation of how it happens in my case). But I
believe this is likely to happen easily when the underlying transport
channel is flaky. For instance, the local TCP end receives some data,
but then the TX window doesn't advance and data piles up in the TX
queue until it fills up the queue.

I am unfamiliar with the libssh2 code base, so I might be missing
something. Please let me know if this makes any sense. Thanks!

Best regards,
Radu Rendec
_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

