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

Re: [libssh2] Hang on nonexistent scp file download

From: Dan Fandrich <>
Date: Mon, 26 Mar 2007 17:09:24 -0700

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:

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 26 Mar 2007 23:56:17 -0000
@@ -1457,8 +1466,8 @@ LIBSSH2_API int libssh2_channel_free(LIB
         /* Clear out packets meant for this channel */
         libssh2_htonu32(channel_id, channel->;
- 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 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.

>>> Dan

--              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's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
libssh2-devel mailing list
Received on 2007-03-27