Subject: Re: [libssh2] Hang on nonexistent scp file download

Re: [libssh2] Hang on nonexistent scp file download

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Fri, 30 Mar 2007 21:58:55 -0700

On Mon, Mar 26, 2007 at 05:09:24PM -0700, Dan Fandrich wrote:
> On Thu, Mar 22, 2007 at 04:44:48PM -0700, Dan Fandrich wrote:
> > If I run the unmodified example/simple/scp program (CVS version), specifying
> > a valid username and password on the command line and an invalid file name,
> > the program hangs. The final output is:
> [...]
> > It seems that libssh2_channel_free is waiting for SSH_MSG_CHANNEL_EXTENDED_DATA
> > that is never received. If I specify a valid file name, all is well.
>
> I've solved this problem with this patch:
[...]
> This sets the poll_socket flag on these calls to false. Otherwise, when
> libssh2_channel_free is called from libssh2_scp_recv, it blocks while
> waiting for a data packet that will never arrive because the channel has
> already been closed.
>
> I'm not quite sure under which cases the poll_socket flag ought to be set--
> I would have thought it should be a nonblocking operation, otherwise, callers
> should be using libssh2_packet_require_ex instead of libssh2_packet_ask_ex.
> I'm not sure what other implications this patch has. Maybe only the second
> line above should be called with poll_socket 0--that fixes the problem as well.

After some more reflection, it looks to me like this patch is the right
way to go. The purpose of these calls is to clear out any packets that
may have arrived already, so there's no need to poll for more. There's
a similar loop in libssh2_channel_free that hasn't caused me a problem yet,
but looks like it suffers from the same problem.

The following patch fixes both sections (please ignore the fuzz).

Index: channel.c
===================================================================
RCS file: /cvsroot/libssh2/libssh2/src/channel.c,v
retrieving revision 1.38
diff -u -p -r1.38 channel.c
--- channel.c 7 Feb 2007 21:42:45 -0000 1.38
+++ channel.c 31 Mar 2007 04:40:18 -0000
@@ -317,10 +317,10 @@ libssh2_channel_open_ex(LIBSSH2_SESSION
                 libssh2_htonu32(channel_id, channel->local.id);
                 while ((libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_DATA,
                                                &data, &data_len, 1, channel_id,
- 4, 1) >= 0) ||
+ 4, 0) >= 0) ||
                         (libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_EXTENDED_DATA,
                                                &data, &data_len, 1, channel_id,
- 4, 1) >= 0)) {
+ 4, 0) >= 0)) {
                         LIBSSH2_FREE(session, data);
                 }
 
@@ -1457,8 +1466,8 @@ LIBSSH2_API int libssh2_channel_free(LIB
 
         /* Clear out packets meant for this channel */
         libssh2_htonu32(channel_id, channel->local.id);
- while ((libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_DATA, &data, &data_len, 1, channel_id, 4, 1) >= 0) ||
- (libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data, &data_len, 1, channel_id, 4, 1) >= 0)) {
+ while ((libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_DATA, &data, &data_len, 1, channel_id, 4, 0) >= 0) ||
+ (libssh2_packet_ask_ex(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data, &data_len, 1, channel_id, 4, 0) >= 0)) {
                 LIBSSH2_FREE(session, data);
         }

This e-mail thread gets pretty lonely when I'm the only one talking.
This is causing me real problems--I've avoided putting a test for this into
curl's test suite because it causes a total hang. The protocol violation
on close I mentioned in another thread causes intermittent problems,
but there is now a test for it in curl; it can be seen as a test 603
failure in the curl autobuild page at http://curl.haxx.se/auto/ .

>>> Dan

-- 
http://www.MoveAnnouncer.com              The web change of address service
          Let webmasters know that your web site has moved
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
libssh2-devel mailing list
libssh2-devel_at_lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libssh2-devel
Received on 2007-03-31